Mixin pattern with ECS in Godot for "FoodSources"


Adding “FoodSources”

  • BerryBush
  • OrangeTree

This demo shows random spawn positions. each morning (based on the day/levels we set in the game db) food sources will spawn an amount of food. BerryBush.spawn(4) will “drop” 4 berries that can be picked up

Player can pickup food.

This also demonstrates a way to render the UI. If an action is available, then the button to do it appears.

ECS and Mixins

ECS refers to “Entity Component System” which is a software architectural pattern used by some game engines such as Unity and Godot.

An Entity is a general-purpose object that can be a 2D Sprite, a 3D StaticBody, an Audio clip, a Script, a generic empty Node, etc.

A Component is the raw data for one aspect of the object, and how it interacts with the world.

Each Entity runs as if it was it’s own System, with it’s own setup, loop, and teardown lifecycle functions.

A Mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes.

I added berry bushes that spawn berries that could be picked up. There are other food sources that can spawn items, so naturally i created a FoodSource super class.

I added a crafting bench that a player can stand near and emit signals, such as “i’m standing next to crafting bench, show the crafting button” There are other structures that a player can stand near and emit signals, so naturally -> Structure super class.

This worked really well

┌────────────┐ ┌───────────────┐
│            │ │               │
│ FoodSource │ │   Structure   │
│            │ │               │
└──────┬─────┘ └───────┬───────┘
       │               │
┌──────▼─────┐ ┌───────▼───────┐
│            │ │               │
│ BerryBush  │ │ CraftingBench │
│            │ │               │
└────────────┘ └───────────────┘

However, I created a BasicTree which is a structure that a player can stand near and can spawn items that a player can collect (wood). GDScript doesn’t have native prototypical inheritance, native mixins, or native decorators (it’s duck-typed which is pretty powerful)

We can leverage the ECS nature of godot to implement mixins.

I created a src/Mixins/ directory that contains scenes which can be added to any node tree to provide these behaviors.

   ┌───────────┐                ┌───────────────┐
   │           │                │               │
   │  Spawner  │                │  Structure2D  │
   │           │                │               │
   └────▲───▲──┘                └───▲────▲──────┘
        │   │                       │    │
        │   └────────┐      ┌───────┘    │
        │            │      │            │
┌───────┴─────┐  ┌───┴──────┴──┐ ┌───────┴─────────┐
│             │  │             │ │                 │
│  BerryBush  │  │  BasicTree  │ │  CraftingBench  │
│             │  │             │ │                 │
└─────────────┘  └─────────────┘ └─────────────────┘

Documentation

Mixins

These nodes provide behaviors to other nodes by adding them to their ECS tree.

They will require some “setup” to use and will be documented here.

Spawner

spawner screenshot

node group

Provides random spawns that player can pickup. Add Position2D nodes as a child of the Spawner, then add them to group “random_spawn_points”

Interface

reset_spawns()
spawn(count: int = 1)

Structure2D

structure2d screenshot

Provides signals that the player is in it’s activation area

Structure2D requires a child node Area2D named ‘ActivationArea2D’

Props

  • structure_name: String
  • type: StructureType (enum)

Signals

  • Globals.emit_player_entered_structure_activation_area(player, structure)
  • Globals.emit_player_exited_structure_activation_area(player)

Get Camp Fire Craft

Leave a comment

Log in with itch.io to leave a comment.