Posted February 16, 2022 by GomaGames
#learn a skill #godot #coding
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
This also demonstrates a way to render the UI. If an action is available, then the button to do it appears.
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 │
│ │ │ │ │ │
└─────────────┘ └─────────────┘ └─────────────────┘
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.
Provides random spawns that player can pickup. Add Position2D nodes as a child of the Spawner, then add them to group “random_spawn_points”
reset_spawns()
spawn(count: int = 1)
Provides signals that the player is in it’s activation area
Structure2D requires a child node Area2D named ‘ActivationArea2D’