The DragonScript Scripting Module provides 3 main ways to build your game depending on your skill level and project requirements. The layers are listed from the most simple one (which is recommended for most projects) to the most flexible one.
Behavior Elements are build on top of the Scenery Elements and provide a composeable way to build game logic. This is the fastest way to build your game by befitting elements with reusable behavior while still retaining all the powerful coupling to other script classes.
Behavior elements are created by subclassing from BehaviorElementClass script class. This class extends the ElementClass script class with support to add Behavior Definitions. When a BehaviorElement instance is created these behavior definitions are used to befit the element with all the required functionality. All this can be done manually by using Scenery Elements but using Behavior Elements this boils down to just adding the behavior definitions you like to use.
Behavior definitions are created by subclassing from ECBehavior script class. This stands for Element Class Behavior. Behavior definitions typically contain Composeable Element Class or Element Class Property instances with appropriate parameters set. This has a few nice properties making life simpler.
Element class properties can be used with XML Subclassing. You can created a new element class just using an XML file (*.deeclass) and change the properties added by the behavior definition. This is especially useful for mappers to add variations of Game Objects without needing to touch script code for such a simple task.
Furthermore Element Stubs used to create Game Objects in your game world use the same names as the element class properties. This allows mappers to modify behaviors on a per Game Object basis if required.
You can query BehaviorElementClass if it contains behaviors of a specific type. Some behavior definitions can be used more than once on the same element class. In this case you can assign each instance a unique identifier. This identifier is used to distinguish between the behavior definition instances and acts also as prefix modified of the added element class properties.
Once a BehaviorElement instance is created each behavior definitions adds a subclass of ECBehaviorInstance. These provide the actual behavior to the BehaviorElement instance. Here too you can query the element if it contains instances of a specific behavior.
Behaviors are split in two groups: Basic Behaviors and Complex Behaviors. Basic Behaviors add a small basic building block of game logic like for example a component showing the visual appearance of a game object. These are highly reusable. Complex Behaviors on the other hand typically expect one or more Basic Behaviors to be present in the element class to provide a complex behavior for example animating the visual appearance.
The DragonScript Scripting Module provides these Basic Behaviors as starting help:
The DragonScript Scripting Module provides these Complex Behaviors as starting help:
Many more complex behaviors can be created either just for this game project or to be shared with other projects. Behaviors can extend other behaviors by subclassing or by using other behaviors present in the behavior element class.
This is an example of an element class which which has a visual appearance, physical interaction, is animated, has two lights, has a speaker and moves along a rail. Basically this describes a kind of train running on a rail between two destinations.
pin Dragengine.Gui pin Dragengine.Scenery pin Dragengine.Utils class MyTrainClass extends BehaviorElementClass public var ECBehaviorComponent component // visual appearance public var ECBehaviorAnimated animated // animated the visual appearance public var ECBehaviorCollider collider // physical interaction public var ECBehaviorMoveOnRail moveOnRail // move the collider on along a rail public var ECBehaviorLight headLightLeft // left head light public var ECBehaviorLight headLightRight // right head light public var ECBehaviorSpeaker speaker // engine sound public func new() super("MyTrainTest") // create visual appearance with default model(mesh), skin and rig. the rig is used both for // physical interaction and animation purpose component = ECBehaviorComponent.new(this) component.getComponent().getModel().setPath("/content/train/train.demodel") component.getComponent().getSkin().setPath("/content/train/train.deskin") component.getComponent().getRig().setPath("/content/train/train.derig") // create collider which is used for physical interaction collider = ECBehaviorCollider.new(this) // create head lights. attached to collider defined above headLightLeft = ECBehaviorLight.new(this) headLightLeft.getLight().getColor().setColor(Color.YELLOW) headLightLeft.getAttach().getPosition().setVector(Vector.new(-1.0, 0.5, 2.0)) headLightRight = ECBehaviorLight.new(this) headLightRight.getLight().getColor().setColor(Color.YELLOW) headLightRight.getAttach().getPosition().setVector(Vector.new(1.0, 0.5, 2.0)) // create engine speaker. attached to the collider defined above speaker = ECBehaviorSpeaker.new(this) speaker.getSpeaker().getSound().setPath("/content/train/engine.ogg") speaker.getAttach().getPosition().setVector(Vector.new(0.0, 0.5, 1.5)) // create behavior animating the visual appearance using an animator animated = ECBehaviorAnimated.new(this) animated.getAnimator().getAnimator().setPath("/content/train/train.deanimator") // create behavior moving collider on rail. the mapper defines what rail to use moveOnRail = ECBehaviorMoveOnRail.new(this) end end
If you want to also add run-time behavior on top of the behavior without creating your own behavior you can modify the code like this:
class MyTrainClass extends BehaviorElementClass ... protected func Element createElement() return MyTrain.new(this) end end class MyTrain extends BehaviorElement public func new(MyTrainClass eclass) super(eclass) ... end end