Compare commits
3 Commits
1c1d8bf3aa
...
game-of-li
| Author | SHA1 | Date | |
|---|---|---|---|
| 9f36f7c921 | |||
| f9ec2e855d | |||
| c92c6d4432 |
4
.gitmodules
vendored
Normal file
4
.gitmodules
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
[submodule "game-of-life-test/thirdparty/godot-cpp"]
|
||||||
|
path = game-of-life-test/thirdparty/godot-cpp
|
||||||
|
url = https://github.com/godotengine/godot-cpp.git
|
||||||
|
branch = 4.5
|
||||||
@@ -6,12 +6,21 @@ var arr := []
|
|||||||
var data_img: Image
|
var data_img: Image
|
||||||
var data_tex: ImageTexture
|
var data_tex: ImageTexture
|
||||||
|
|
||||||
|
var gol := GoL.new()
|
||||||
|
|
||||||
@onready var mat: ShaderMaterial = material as ShaderMaterial
|
@onready var mat: ShaderMaterial = material as ShaderMaterial
|
||||||
|
|
||||||
# sim. consts
|
# sim. consts
|
||||||
var T := 0.1
|
var T := 0.01
|
||||||
var t := 0.0
|
var t := 0.0
|
||||||
|
|
||||||
|
# player (2x2 block)
|
||||||
|
var player_alive := true
|
||||||
|
var player_col := 10
|
||||||
|
var player_row := 10
|
||||||
|
var PLAYER_SHAPE := [Vector2i(0,0), Vector2i(1,0), Vector2i(0,1), Vector2i(1,1)]
|
||||||
|
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# Called when the node enters the scene tree for the first time.
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
data_img = Image.create(n, n, false, Image.FORMAT_R8)
|
data_img = Image.create(n, n, false, Image.FORMAT_R8)
|
||||||
@@ -21,8 +30,10 @@ func _ready() -> void:
|
|||||||
mat.set_shader_parameter("binDataTex", data_tex)
|
mat.set_shader_parameter("binDataTex", data_tex)
|
||||||
mat.set_shader_parameter("n", n)
|
mat.set_shader_parameter("n", n)
|
||||||
|
|
||||||
# init
|
# player
|
||||||
|
_seed_clear()
|
||||||
_fill_example()
|
_fill_example()
|
||||||
|
_spawn_player(20, 10)
|
||||||
_upload_arr()
|
_upload_arr()
|
||||||
|
|
||||||
|
|
||||||
@@ -35,10 +46,21 @@ func _upload_arr() -> void:
|
|||||||
|
|
||||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
func _process(_delta: float) -> void:
|
func _process(_delta: float) -> void:
|
||||||
|
if Input.is_action_just_pressed("move_right"):
|
||||||
|
_move_player(1, 0)
|
||||||
|
if Input.is_action_just_pressed("move_left"):
|
||||||
|
_move_player(-1, 0)
|
||||||
|
if Input.is_action_just_pressed("move_up"):
|
||||||
|
_move_player(0, -1)
|
||||||
|
if Input.is_action_just_pressed("move_down"):
|
||||||
|
_move_player(0, 1)
|
||||||
|
|
||||||
t += _delta
|
t += _delta
|
||||||
if t >= T:
|
if t >= T:
|
||||||
t = 0.0
|
t = 0.0
|
||||||
_game_of_life_step()
|
_game_of_life_step()
|
||||||
|
# arr = gol.step_once(arr, n) # cpp step (no player)
|
||||||
|
_track_player_after_step()
|
||||||
_upload_arr()
|
_upload_arr()
|
||||||
|
|
||||||
func _game_of_life_step() -> void:
|
func _game_of_life_step() -> void:
|
||||||
@@ -72,8 +94,35 @@ func _count_neighs(x:int, y:int) -> int:
|
|||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
func _track_player_after_step() -> void:
|
||||||
|
if not player_alive:
|
||||||
|
return
|
||||||
|
var best_count := -1
|
||||||
|
var best_shift := Vector2i(0, 0)
|
||||||
|
|
||||||
|
# search a 5x5 neighborhood for where the 2x2 footprint moved
|
||||||
|
for dy in range(-2, 3):
|
||||||
|
for dx in range(-2, 3):
|
||||||
|
var cnt := 0
|
||||||
|
for off in PLAYER_SHAPE:
|
||||||
|
var x:int = (player_col + off.x + dx + n) % n
|
||||||
|
var y:int = (player_row + off.y + dy + n) % n
|
||||||
|
cnt += arr[y][x]
|
||||||
|
if cnt > best_count:
|
||||||
|
best_count = cnt
|
||||||
|
best_shift = Vector2i(dx, dy)
|
||||||
|
|
||||||
|
# if nothing from the footprint survived, the player is dead
|
||||||
|
if best_count <= 0:
|
||||||
|
player_alive = false
|
||||||
|
print("Player dead!")
|
||||||
|
return
|
||||||
|
|
||||||
|
player_col = (player_col + best_shift.x + n) % n
|
||||||
|
player_row = (player_row + best_shift.y + n) % n
|
||||||
|
|
||||||
|
|
||||||
func _fill_example() -> void:
|
func _fill_example() -> void:
|
||||||
arr.clear()
|
|
||||||
#_checkerboard()
|
#_checkerboard()
|
||||||
_ship()
|
_ship()
|
||||||
|
|
||||||
@@ -102,3 +151,46 @@ func _ship() -> void:
|
|||||||
arr[2][0] = 1
|
arr[2][0] = 1
|
||||||
arr[2][1] = 1
|
arr[2][1] = 1
|
||||||
arr[2][2] = 1
|
arr[2][2] = 1
|
||||||
|
|
||||||
|
|
||||||
|
# Player
|
||||||
|
func _write_player(val:int) -> void:
|
||||||
|
for off in PLAYER_SHAPE:
|
||||||
|
var x:int = (player_col + off.x + n) % n
|
||||||
|
var y:int = (player_row + off.y + n) % n
|
||||||
|
arr[y][x] = val
|
||||||
|
|
||||||
|
func _spawn_player(col:int, row:int) -> void:
|
||||||
|
player_col = (col + n) % n
|
||||||
|
player_row = (row + n) % n
|
||||||
|
player_alive = true
|
||||||
|
_write_player(1)
|
||||||
|
_upload_arr()
|
||||||
|
|
||||||
|
func _despawn_player() -> void:
|
||||||
|
if player_alive:
|
||||||
|
_write_player(0)
|
||||||
|
player_alive = false
|
||||||
|
_upload_arr()
|
||||||
|
|
||||||
|
func _move_player(dx:int, dy:int) -> void:
|
||||||
|
if not player_alive:
|
||||||
|
return
|
||||||
|
# erase old footprint
|
||||||
|
_write_player(0)
|
||||||
|
# move
|
||||||
|
player_col = (player_col + dx + n) % n
|
||||||
|
player_row = (player_row + dy + n) % n
|
||||||
|
# write new footprint
|
||||||
|
_write_player(1)
|
||||||
|
_upload_arr()
|
||||||
|
|
||||||
|
|
||||||
|
# Clear
|
||||||
|
func _seed_clear() -> void:
|
||||||
|
arr.clear()
|
||||||
|
for _y in n:
|
||||||
|
var row := []
|
||||||
|
for _x in n:
|
||||||
|
row.append(0)
|
||||||
|
arr.append(row)
|
||||||
|
|||||||
23
game-of-life-test/player.gd
Normal file
23
game-of-life-test/player.gd
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
extends Area2D
|
||||||
|
|
||||||
|
@export var speed = 400 # How fast the player will move (pixels/sec).
|
||||||
|
var screen_size # Size of the game window.
|
||||||
|
|
||||||
|
func _ready() -> void:
|
||||||
|
screen_size = get_viewport_rect().size
|
||||||
|
|
||||||
|
func _process(delta):
|
||||||
|
var velocity = Vector2.ZERO # The player's movement vector.
|
||||||
|
if Input.is_action_pressed("move_right"):
|
||||||
|
velocity.x += 1
|
||||||
|
if Input.is_action_pressed("move_left"):
|
||||||
|
velocity.x -= 1
|
||||||
|
|
||||||
|
if velocity.length() > 0:
|
||||||
|
velocity = velocity.normalized() * speed
|
||||||
|
$AnimatedSprite2D.play()
|
||||||
|
else:
|
||||||
|
$AnimatedSprite2D.stop()
|
||||||
|
|
||||||
|
position += velocity * delta
|
||||||
|
position = position.clamp(Vector2.ZERO, screen_size)
|
||||||
1
game-of-life-test/player.gd.uid
Normal file
1
game-of-life-test/player.gd.uid
Normal file
@@ -0,0 +1 @@
|
|||||||
|
uid://cvxhfc50fgdyb
|
||||||
@@ -19,3 +19,26 @@ config/icon="res://icon.svg"
|
|||||||
|
|
||||||
window/size/viewport_width=1920
|
window/size/viewport_width=1920
|
||||||
window/size/viewport_height=1920
|
window/size/viewport_height=1920
|
||||||
|
|
||||||
|
[input]
|
||||||
|
|
||||||
|
move_right={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
move_left={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
move_up={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
move_down={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|||||||
10
game-of-life-test/thirdparty/.gitignore
vendored
Normal file
10
game-of-life-test/thirdparty/.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
bin
|
||||||
|
*.uid
|
||||||
|
*sconsign.dblite
|
||||||
|
**.os
|
||||||
|
|
||||||
|
game-of-life-test/thirdparty/godot-cpp/bin/
|
||||||
|
game-of-life-test/thirdparty/bin/
|
||||||
|
*.o
|
||||||
|
*.so
|
||||||
|
|
||||||
31
game-of-life-test/thirdparty/SConstruct
vendored
Normal file
31
game-of-life-test/thirdparty/SConstruct
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import os
|
||||||
|
from SCons.Script import DefaultEnvironment, ARGUMENTS, Glob, Dir
|
||||||
|
|
||||||
|
env = DefaultEnvironment()
|
||||||
|
platform = ARGUMENTS.get("platform", "linux")
|
||||||
|
target = ARGUMENTS.get("target", "template_debug") # or template_release
|
||||||
|
arch = ARGUMENTS.get("arch", "x86_64")
|
||||||
|
|
||||||
|
HERE = Dir(".").abspath
|
||||||
|
GODOTCPP = os.path.join(HERE, "godot-cpp")
|
||||||
|
|
||||||
|
inc_paths = [
|
||||||
|
os.path.join(GODOTCPP, "include"),
|
||||||
|
os.path.join(GODOTCPP, "gen", "include"),
|
||||||
|
]
|
||||||
|
for extra in ("godot-headers", "gdextension"):
|
||||||
|
p = os.path.join(GODOTCPP, extra)
|
||||||
|
if os.path.isdir(p):
|
||||||
|
inc_paths.append(p)
|
||||||
|
|
||||||
|
env.Append(CPPPATH=inc_paths)
|
||||||
|
env.Append(LIBPATH=[os.path.join(GODOTCPP, "bin")])
|
||||||
|
env.Append(CXXFLAGS=["-std=c++17", "-fPIC"])
|
||||||
|
|
||||||
|
env.Append(LIBS=[f"godot-cpp.{platform}.{target}.{arch}"])
|
||||||
|
|
||||||
|
outdir = os.path.join(HERE, "bin")
|
||||||
|
os.makedirs(outdir, exist_ok=True)
|
||||||
|
soname = f"gol.{platform}.{'debug' if target == 'template_debug' else 'release'}.{arch}"
|
||||||
|
|
||||||
|
env.SharedLibrary(target=os.path.join(outdir, soname), source=Glob("src/*.cpp"))
|
||||||
1
game-of-life-test/thirdparty/godot-cpp
vendored
Submodule
1
game-of-life-test/thirdparty/godot-cpp
vendored
Submodule
Submodule game-of-life-test/thirdparty/godot-cpp added at abe94570a1
9
game-of-life-test/thirdparty/gol.gdextension
vendored
Normal file
9
game-of-life-test/thirdparty/gol.gdextension
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
[configuration]
|
||||||
|
entry_symbol = "gol_library_init"
|
||||||
|
compatibility_minimum = "4.5" ; required in 4.5+
|
||||||
|
reloadable = true
|
||||||
|
|
||||||
|
[libraries]
|
||||||
|
linux.debug.x86_64 = "res://thirdparty/bin/libgol.linux.debug.x86_64"
|
||||||
|
|
||||||
75
game-of-life-test/thirdparty/src/gol.cpp
vendored
Normal file
75
game-of-life-test/thirdparty/src/gol.cpp
vendored
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
|
||||||
|
// src/gol.cpp
|
||||||
|
|
||||||
|
#include <gdextension_interface.h>
|
||||||
|
#include <godot_cpp/classes/ref_counted.hpp>
|
||||||
|
#include <godot_cpp/core/class_db.hpp>
|
||||||
|
#include <godot_cpp/core/defs.hpp>
|
||||||
|
#include <godot_cpp/godot.hpp>
|
||||||
|
#include <godot_cpp/variant/array.hpp>
|
||||||
|
|
||||||
|
using namespace godot;
|
||||||
|
|
||||||
|
class GoL : public RefCounted {
|
||||||
|
GDCLASS(GoL, RefCounted);
|
||||||
|
|
||||||
|
static void _bind_methods() {
|
||||||
|
ClassDB::bind_method(D_METHOD("step_once", "arr", "n"), &GoL::step_once);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int neighs(const Array &a, int n, int x, int y) {
|
||||||
|
int s = 0;
|
||||||
|
for (int dy = -1; dy <= 1; ++dy) {
|
||||||
|
for (int dx = -1; dx <= 1; ++dx) {
|
||||||
|
if (dx == 0 && dy == 0)
|
||||||
|
continue;
|
||||||
|
const int nx = ((x + dx) % n + n) % n;
|
||||||
|
const int ny = ((y + dy) % n + n) % n;
|
||||||
|
const Array row = a[ny];
|
||||||
|
s += (int)row[nx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
Array step_once(const Array &arr, int n) const {
|
||||||
|
Array next;
|
||||||
|
next.resize(n);
|
||||||
|
for (int y = 0; y < n; ++y) {
|
||||||
|
Array row;
|
||||||
|
row.resize(n);
|
||||||
|
const Array crow = arr[y];
|
||||||
|
for (int x = 0; x < n; ++x) {
|
||||||
|
const bool alive = ((int)crow[x]) == 1;
|
||||||
|
const int cn = neighs(arr, n, x, y);
|
||||||
|
row.set(x, alive ? int(cn == 2 || cn == 3) : int(cn == 3));
|
||||||
|
}
|
||||||
|
next.set(y, row);
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
extern "C" GDExtensionBool GDE_EXPORT
|
||||||
|
gol_library_init(GDExtensionInterfaceGetProcAddress get_proc_address,
|
||||||
|
GDExtensionClassLibraryPtr library,
|
||||||
|
GDExtensionInitialization *r_initialization) {
|
||||||
|
|
||||||
|
static GDExtensionBinding::InitObject init(get_proc_address, library,
|
||||||
|
r_initialization);
|
||||||
|
|
||||||
|
init.register_initializer([](ModuleInitializationLevel p_level) {
|
||||||
|
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE)
|
||||||
|
return;
|
||||||
|
ClassDB::register_class<GoL>();
|
||||||
|
});
|
||||||
|
|
||||||
|
init.register_terminator([](ModuleInitializationLevel /*p_level*/) {
|
||||||
|
// no-op
|
||||||
|
});
|
||||||
|
|
||||||
|
init.set_minimum_library_initialization_level(
|
||||||
|
MODULE_INITIALIZATION_LEVEL_SCENE);
|
||||||
|
return init.init();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user