arkerone Posted June 20, 2015 Share Posted June 20, 2015 Hello everyone, I will develop a new game with phaser, and I ask myself questions about how organize my code. I would like to use the ECS architecture but how implement it with Phaser? how you decouple the differents actors (player, enemies, objects,etc...) in your game? How you implement the differents interactions between the player and the environment (a door for instance)? Thanks! Link to comment Share on other sites More sharing options...
drhayes Posted June 22, 2015 Share Posted June 22, 2015 Here's my approach so far: I made a behavior framework that adds a few methods to certain sprites. The two most important are "behave" and "addBehavior". A behavior is a separate class that is added to a sprite that encapsulates a single behavior, like walking or getting hurt. Each behavior instance has an "enabled" property. The "behave" method iterates over all added behaviors in a sprite and invokes them if they are enabled. In my sprite's update method I have a call "this.behave('update');" which, in turn, invokes a behavior's update method like this: "behavior.update(sprite);". "behave" also lets me pass arguments like this: "behave('onAnimationComplete', animation);" which turns into "behavior.onAnimationComplete(sprite, animation);". Each behavior can then maintain its own state and use the "parent" sprite to communicate with other behaviors. It can also easily be parameterized by passing in values in the constructor. My player is my most complex sprite with around 14 behaviors (and counting). One of them is PlayerGroundMove which checks if the player is alive or hurt before allowing it to move. It also resets the inputs when the player sprite is killed. I'm making a platformer RPG-ish game. A more useful, re-usable one is "StayOnPlatform". As the sprite moves it checks the collision layer in front of and below the sprite. If there's no floor, the sprite turns around and keeps walking in the other direction. There's also "DamageOrDie" which checks the player state. If the player has a particular item then the sprite dies; otherwise, the player dies. In both cases changing the single behavior changes all sprites that use that behavior. Kind of a "rising tide lifting all boats" thing. Between those two making new enemies is a snap and my code is super focused on any particular new problem. For example, if there's a problem with what happens when the player falls to its death I don't have to go searching through a super-long "update" method, or even dig around in my Player class – I just head to my "PlayerFallingDeath" behavior and fix it in there. Link to comment Share on other sites More sharing options...
Recommended Posts