- // I forget the proper java syntax, so convert it as needed.
- // this is the base event class which will contain basic information that every event type must use.
- public class baseEvent
- {
- public int time;
- public baseEvent() {} // default constructor
- public baseEvent(int time) { this.time = time; } // constructor with basic information
- public void process() {} //more commonly called 'run()'
- }
- // based off of the usages, lets create three separate classes based off of these.
- public class setActionEvent extends baseEvent
- {
- public entity sourceEntity;
- public String actionEvent;
- public setActionEvent() { super(); }
- public setActionEvent(int time, entity source, String actionEvent) {
- super(time);
- this.sourceEntity = entity;
- this.actionEvent = actionEvent;
- }
- @Override
- public void process() { this.sourceEntity.setAction( this.actionEvent ); }
- }
- public class deleteEnemyEvent extends baseEvent
- {
- public entity sourceEntity;
- public deleteEnemyEvent() { super(); }
- public deleteEnemyEvent(int time, entity source) {
- super(time);
- this.sourceEntity = entity;
- }
- @Override
- public void process() { sourceEntity.delete(); }
- }
- public class createExplosionEvent extends baseEvent
- {
- public entity sourceEntity; // the entity which caused the explosion (if needed.)
- public Vector2D position;
- public createExplosionEvent() { super(); }
- public createExplosionEvent(int time, entity source, Vector2D position) {
- super(time);
- this.sourceEntity = entity;
- this.position = position;
- }
- @Override
- public void process() { ExplosionFactory.spawn( this.position, this.sourceEntity ); } //whatever call is needed to spawn/create an explosion, a pointer to this class could even be stored in this class.
- }
- // you would then have a collection of baseEvents, in your example called EventQueue. Equal examples of using these three event types is as follows:
- EventQueue.Add( new setActionEvent( GS.clock() + x, this, "hit" ) ); //'this' will be the player.
- EventQueue.Add( new deleteEnemyEvent( GS.clock() + 250, this ) ); //'this' will be the enemy.
- EventQueue.Add( new createExplosionEvent( GS.clock() + (i*73), this, b ) ); //'this' will be the entity which caused the explosion (if needed.)
- // Notes:
- // * Any data not needed can be ignored, simply an example.
- // * Using 'this' inside the class is purely a habit of mine, can be ignored.
- // * This is a lot more work then doing simple function calls, but this is only way around not being able to call/store function pointers. this is the reason why object orientated languages was created. the only real benefit of this way is to control the event class for each type of event.
- // * Can also rename 'setActionEvent()' to 'startSpriteAnimationEvent()'.