In C3 we now have the option to subclass objects. This essentially means writing a class that extends an object class and will allow us to add custom code to the already present object's code.
What this can be used for is pretty straightforward: If you want to replace the code you have in your events with JS, you need a way to access stuff like object position right?
Anyway there is one issue with that system being that it only allows us to add one class to our object, which might be bad in case we want to have a family like system where objects can be added to many different groups that all have a defined behavior.
SpriteBase.js
In order to make that possible the first class you want to write should be one that acts as a middleman between your behavior classes and your object. Something like that should work:
class SpriteBase extends ISpriteInstance {
constructor(){
super()
}
addBehavior(name, behavior){
if(!this.hasOwnProperty(name))
this[name] = behavior
}
}
This is a super simple code that just allows further subclasses to link other classes to the object, basically acting like a behavior system
Behaviors
Now let's say I have a battle system and I want some objects to have access to battle system features but I also want them to be able to do other things.
I'll start by writing a BattleEntity class:
class BattleEntity {
constructor(inst){
this.inst = inst
this.runtime = inst.runtime
this.skills = []
}
execSkill(id){
return this.runtime.startCoroutine(this.skills[id].actions(this, this.inst))
}
}
Subclassing the subclass
Now I want to create a class that extends the SpriteBase class and adds a BattleEntity behavior to it
class Pawn extends SpriteBase {
constructor(){
super()
this.addBehavior("battle", new BattleEntity(this))
}
}
If I ever want to add other features to my Pawn, I can either add them directly in the class, or I can add them in a separate class and add it as a behavior.
Subclassing the subclass of a subclass...?
I can also decide to use that Pawn class as a base class for other battle entities
class Watcher extends Pawn {
constructor(){
super()
this.battle.skills = [
this.runtime.battleSystem.skills.watcherSkill1,
]
}
}
Anyway...
Now you have some code that's modular extendable and allows for a bit more organisation in your code.