A falling sand game made with Godot 4.0's GDextension feature. The full game can be downloaded for free on itch.io or on the Google Play Store.
The project was initialized using a GDExtension template, GDSummator..
The simulation logic is implemented in C++ as a GDextension (in extension
) that gets compiled into a binary file (in game\bin
).
The user interface was created in a Godot 4.0.3 project (in game
).
- Clone the sand-slide repository
- In a command prompt, navigate to the
godot-cpp
folder and runscons
(addtarget=template_release
for exported projects, orplatform=...
for other devices). You only need to do this the first time you initialize the repository. Note that theMD
flag ingodot-cpp\tools\targets.py
will prevent the game from running on Windows devices without Visual Studio installed. You can change all instances ofMD
toMT
to fix this. If you recieve anAccess Denied
error, make sure that the project is not open in another window while compiling. - You are now ready to make changes to the project!
To compile any changes, navigate to the root of the project and run scons
.
The binaries inside of game\bin
will update and the changes can be observed once the project is reloaded.
Each element is a class within extension\elements
that inherits the Element
class.
To create your own element, create a new .h
file and define a class with the name of your element.
Your element must inherit the Element
class and implement the virtual methods process(*SandSimulation sim, int row, inc col)
, get_explode_resistance()
, and get_acid_resistance()
.
process(*SandSimulation sim, int row, inc col)
contains the main logic for your element.
As the simulation iterates through each cell, it will call process
on the respective Element
class and pass the row
and col
that the cell was encountered in.
Elements have no data or information associated with them, so process
is essentially a static method that must infer
state from surrounding cells. You can get information and manipulate the simulation by calling methods from the SandSimulation
pointer. Here is an example for sand:
const double FLAME = 1 / 64.0;
void process(SandSimulation *sim, int row, int col) override {
// randf is a helper method defined within sand_simulation.h that returns a random real number [0, 1)
if (sim->randf() < FLAME && sim->is_on_fire(row, col)) {
// Set this cell to glass when on fire
sim->set_cell(row, col, 25); // 25 is the ID of glass, as explained further below
return;
}
// Otherwise, keep falling
sim->move_and_swap(row, col, row + 1, col);
}
More helper methods can be found in extension\sand_simulation.h
.
get_explode_resistance()
and get_acid_resistance()
must return a float [0, 1] in which 0 is no resistance to explosions/acid and 1 is maximum resistance to explosions/acid. get_temperature()
must return an integer, -1 being cold, 0 being neutral, and 1 being hot. get_toxicity()
must return an integer as well, 1 being toxic and 0 being neutral.
When you are ready to add your element to the game, import the header file in extension\elements\all_elements.h
and add it to the elements
vector within the fill_elements
method.
The index that you add the element to will become its ID
. Other elements reference this ID
when checking conditions, such as above where sand is replaced with glass. Glass is placed at the 25th spot of the elements
vector, so the ID
of glass is 25.
In the Godot project, create a new ElementButton
within the ElementSelector
scene (main\ui\element_selector\ElementSelector
) and set ID
to the same one as defined above. The visuals of your element can be modified by creating a resource that extends the ElementVisual
class and placing it within game\main\element_visuals\
.
The game allows you to manage save files. Files are stored in user:\\
, or %APPDATA%\Godot\app_userdata\sandslide
on Windows. Each folder corresponds to a save file and contains a Godot resource and an image. The luminosity value of each pixel in the texture represents its ID, allowing you to edit the file in an external program and load it back into the game.
Plumbing, eraser, file, support, help and exit icons by Icons8.
Implementation of Bresenham's Line Algorithm by Saideep Dicholkar
GDExtension Starter Project by paddy-exe