name: create-new-affect description: How to create a new type of affect
Background information
Affects are functions that update an objects state. Examples include velocity, which causes an object to move in a particular direction, and bounce, which causes the object to bounce off of the edges of the game world.
Creating a new affect
- Review the circle game object's velocity affect as an example:
- Definition:
src/game/affect.velocity.ts - Usage:
src/game/object.circle.ts
- Definition:
- The affect should define a state object in
src/game/type.object.tswhich extendsAffectStateand sets thecategoryproperty to thez.literalof the newly defined category. - Choose a name for the game affect based on the it's behavior.
- Add the name to the
AffectCategoryenum insrc/game/game.affect.ts - Create the file
src/game/affect.some-new-affect.tswith an exported function that implements theaffectinterface as defined insrc/game/game.affect.ts. - The affect function should be implemented based on the needed behavior.
- Update the
updateStatemethod in thesrc/game/game.object.tsto apply the new affect.
export const BounceState = AffectState.extend({
category: z.literal(AffectCategory.enum.Bounce),
});
export type BounceState = z.infer<typeof BounceState>;
Notes
- Some affects update other affects. Review the target affect for an example of this.
- Some affects interact with game object properties directly. Review the velocity affect for an example of this.
- Some affects use game world properties. Review the bounce affect for an example of this.
- The state for the new affect can include other values, such as strength of gravity, xy coordinates, dx dy velocity, or whatever is needed for that affect.