Jump to content

Decoupling the differents actors on the game


arkerone
 Share

Recommended Posts

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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...