Best way to implement (simple) events/triggers in my platformer game

Sorry, I’m not good at being brief. You can probably get a good enough idea of what I need by only reading half of this but if you read the whole thing you’ll really understand my specific situation and be able to give me very relevant advice. I really appreciate any help I receive as I want to get started coding this soon and I want to employ the help of this community to make better design decisions than I would if I were doing this completely alone.

 

I’m working on the editor for an android/ios/pc game I’m making(with libgdx),

 

(side question: when making a game that has a level editor I usually start by making the editor first and then copy the project and building the actual game off of the editor codebase after the editor is already working, is this a good approach? I like having the game playable/testable from inside the editor so most the gameplay code is already there).

 

It’s just a 2d heavily physics-based platformer with an interesting main concept/gameplay but technically it’s nothing to complicated. The least trivial part of the code is the part this thread is about, the whole event/trigger system. I’ll give some examples to explain what I mean by event/trigger system in my game:

 

The player is running along the obstacle course of a map that exists in my game and steps on a floor platform that looks slightly different from all the other ones around it, this triggers a laser to spawn behind the player, it stands vertically on the platform and starts moving towards the player at a steady speed. After 10 seconds, or when the laser reaches a predefined point, it despawns.

 

Another example would be the player is running along the same level and everything is going fine but he comes into contact with an invisible ‘trigger object'(which is just a rectangular object with no graphic that I placed on the level solely to trigger an event when the player unknowingly ‘collides’ with it) that is placed directly below a platform with a huge boulder on it. When the player hits the transparent rectangle it starts a 1.5 second timer and when that timer runs out the platform above crumbles causing the boulder to fall down potentially crushing the player.

 

 

 

I am just looking for the best way to implement this kind of system. I want it to be flexible because I like to be able to try out many different ideas once my game is completed enough to prototype a bunch of different ideas, but I do know for the most part what kinds of things I want these events to do: enable/disable an object, set a movepath(a set of points, in order, with a set speed, which any game object(platform/obstacle/whatever) will follow in order when it’s set to that movepath, apply a force to an object(I’m using box2d for physics so that’ll be easy), and anything else I think of(any general ideas anyone?). An event should have one or multiple of these ‘actions’ and the ability to happen instantly when triggered, after a preset time interval, or after an animation ends(e.g for the crumbling platform it’d wait a second, play the animation, then at the end of animation despawn the platform).

 

 

My current plan:

I will have an ‘event editor’ window(probably just a JFrame with swing components) that I can open from my main editor window. the event editor will list all the events for that level. Each event will consist of a list of ‘actions.’

First you create a new event, then click add action. select the action type(enable, disable, apply force, set movepath, etc) and depending on the action type it’ll display different options/properties. E.g for set movepath it might give you the options target object, movepath#

(movepaths will be part of the object they belong to, objects can have multiple movepaths and only 1 active at a time), repeating/looping. Each action will also have properties for general things like delay time. you can add as many actions as you want to an event.

 

The whole event will also have some options such as the trigger. You can select the trigger from a list of ‘triggers’ you created in the editor. To create a trigger you can select an object in the editor(e.g the invisible rectangle or the strange platform) and in the objects properties window(also a JFrame that you can open from the main editor window) click create collision trigger and then type in a name for it so it’s easy to find later when assigning it to an event(hmm, maybe for collision trigger I’ll also have an option to select the object it needs to collide with to trigger a collision, so events can be triggered by objects touching other objects instead of just the player touching an object). So far collisions are the only triggers I’ve thought of, but I just had the idea of having an ‘event trigger’ action so I can chain events together, I don’t know if that would be useful though or if I might as well just add more actions to the first event instead of having it trigger a second event with more actions. If anyone has any fun ideas for things that might trigger an event in a typical platformer let me know.

 

 

 

If i go with this idea(right now that’s what I’m planning on doing, although i fear it won’t be flexible enough) I’ll implement it by having ‘Action,’ ‘Event,’ and ‘Trigger’ objects:

 

 

Game objects that trigger events have ‘Trigger’ objects. When player touches one of these gameobjects the object will call activate() on all it’s trigger objects. A trigger object has a list of events and its activate() method just calls start() on all these events.

 

The start() method of an event will add itself to the ‘activeEvents’ arraylist which is located in the same class as my main gameloop. the gameloop calls the update() method on all events in the activeEvents arrayList. Event.update() first takes deltaTime as a parameter and adds that time to the ‘runningTime’ variable of the event class. Then the update method loops through the ‘unactivatedActions’ arraylist in the event class and if runningTime is greater than the delay time for that action it’ll call that actions activate() method and move it from unactivatedActions to the activatedActions array. once the unactivatedActions arraylist is empty it calls the finish() method on the event, this method removes the Event from the activeEvents array in the gameloop class, switches the activatedActions and unactivatedActions arrays back, and does whatever else I’ll need to do when an event finishes.

 

 

The level file will save all this information(‘l’ll probably save levels as JSON, but if anyone wants to make an argument for XML or something else please do so). a level file will consist of a list of GameObjects listed by ID and their properties, trigger id’s+properties, event id’s+properties, and Action Id’s+properties. the json for a gameobject will store the location of the physics body file in my assets folder(I use some physics body editor which stores a box2d body and it’s corresponding .png graphic), location, rotation speed, density/restitution/friction values, a list of movepaths(each movepath being a list of points, each point being a location, movespeed, and pauseTime), and a list of triggers id’s. triggers will be stored by there id and consist of a list of the id’s of the events it triggers. events will be stored by id and consist of a list of action id’s. actions will be stored by id and store the delay time followed by the action type and a list of properties that pertain to that action type(e.g movepath# for movePath Actions).

 

Loading a level will work like this,

load all objects first and store the trigger id’s in the objects. Then load all the triggers, events, and actions into arrays where the array index corresponds to the id’s. then loop through each object and for each object loop through all the trigger id’s, for each trigger id copy the trigger object from the trigger array we just created to the objects trigger array, and as soon as a trigger is added loop through all it’s event id’s, grab those events from the event array i just loaded from the file and add those to the event array in the trigger class, for each event added loop through the events action id’s and copy all the correct actions from the action array I just created to the action array inside the event.

 

Hmmmmm.. or I could just load all the objects and store the trigger id’s in the objects. and load all the triggers/events/actions into separate arrays that are held in my game class and just use the id’s to do everything. So the object only has a list of trigger id’s that point to indices in the main Trigger array, and the Trigger objects in the Trigger array just have int id’s pointing to Events from the main event array and so on, and just code my logic to work off the id’s and Trigger/Event/Action arrays instead of having the triggers belong to objects, events belong to triggers, and actions belong to events. Can someone help me figure out the pros and cons of each of these methods I really don’t understand. Or point out flaws in my ideas/suggest better ideas for me. ANYTHING that will help me design my game/code better

 

Thanks Gamedev