austingray / canvas-game-engine

An HTML 5 canvas game engine.
2 stars 0 forks source link

Need a clear flow of game logic #6

Open austingray opened 5 years ago

austingray commented 5 years ago

Something I'm missing is a very clear flow of game logic. My mental model of this game is getting convoluted. Putting some thought to paper might help resolve that and make adding and refactoring a simpler process.

After writing that first paragraph I came up with this: image

It has already helped me identify some weird issues with how this thing is structured. Mouse input handling is calculated in the draw() method of the Map class. And the logic was just hacked in there so it is horribly unoptimized (it loops through every single visible object 60 times per second.)

TODO: create another github issue prescribing new handling for the mouse: mouse position is set onMouseMove and stored as globally accessible variables, then inside each draw method per object it can check if it is intersecting with the mouse.

Another thing I need a better handle of in my mental model is character input handling. I think by refactoring the input handling in #5 and providing a clear interface to the characterBaseClass, the logic flow will be much more apparent. Especially considering that in the current implementation each character essentially has their own internal loops running via timeouts. Too convoluted.

Another source of convolution is the entire Canvas system. The Canvas.Camera, the Canvas' Layer system, the 2d/3d nature of different layers (which is being addressed in #2). We are missing areas for optimization on a per Layer basis. We need to optimize draws per Layer. Objects should have a way to notify the layers its drawn to that it has changed and needs a redraw. It can trigger a 'needsRedraw' prop on the layer which will then cause it to be drawn in the next cycle.

TODO: update #3 with clear ideas for adding selective redraws.

The goal here is to have a clear flow chart of game logic. Part of this is to update my classes to have clear(er) interfaces for interacting with other parts of the system with a primary focus of maintaining that clean and consistent logic flow.

To be continued.

austingray commented 5 years ago

@minauteur mentioned in chat the idea of having a state management system (citing the idea of a rewind potion and saving/loading games). I think a global game state is a great idea.

TODO: open a github issue for adding a global state to the game.