From 11c4c918756d9496933245db2d87c0b6a4cffbca Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 11 Aug 2023 10:53:00 -0400 Subject: [PATCH] Initial commit - Only supports world generation --- .gitignore | 36 ++++++++++++++++++++++++++++++++++++ README.md | 0 icon.svg | 1 + icon.svg.import | 37 +++++++++++++++++++++++++++++++++++++ project.godot | 24 ++++++++++++++++++++++++ world.gd | 46 ++++++++++++++++++++++++++++++++++++++++++++++ world.tscn | 14 ++++++++++++++ 7 files changed, 158 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 icon.svg create mode 100644 icon.svg.import create mode 100644 project.godot create mode 100644 world.gd create mode 100644 world.tscn diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..667a0eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +# ---> Godot +# Godot-specific ignores +.import/ +export.cfg +export_presets.cfg +.godot/ + +# Imported translations (automatically generated from CSV files) +*.translation + +# Mono-specific ignores +.mono/ +data_*/ + +# Misc +tmp/ + +# ---> Vim +# Swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/icon.svg b/icon.svg new file mode 100644 index 0000000..b370ceb --- /dev/null +++ b/icon.svg @@ -0,0 +1 @@ + diff --git a/icon.svg.import b/icon.svg.import new file mode 100644 index 0000000..759bd0d --- /dev/null +++ b/icon.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://c2vm5pfsamed4" +path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.svg" +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/project.godot b/project.godot new file mode 100644 index 0000000..e373a96 --- /dev/null +++ b/project.godot @@ -0,0 +1,24 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="Conway's Game of Life" +config/features=PackedStringArray("4.1", "Forward Plus") +config/icon="res://icon.svg" + +[input] + +Pause={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194313,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} diff --git a/world.gd b/world.gd new file mode 100644 index 0000000..902730f --- /dev/null +++ b/world.gd @@ -0,0 +1,46 @@ +## Conway's Game of Life +## Rules: +## 1. Any cell with >2 neighbors survives +## 2. Any dead cell with 3 live neighbors becomes alive +## 3. All other cells die (all dead cells remaing dead) +extends Node2D + +@export var world_seed: int +@export var cell_size: Vector2 = Vector2(16, 16) +@export var cell_texture: Texture2D +@export var world_size: Vector2 = Vector2(128, 128) + +var cell_instance +var generation: int = 1 +var world: Array = [] + + +func _ready() -> void: + if world_seed: seed(world_seed) + generate_world() + + +# Create the cell using the rendering server +func create_cell(location: Vector2) -> RID: + var rs = RenderingServer + cell_instance = rs.canvas_item_create() + rs.canvas_item_set_parent(cell_instance, get_canvas_item()) + + var rect: Rect2 = Rect2(-cell_size.x/2, -cell_size.y/2, cell_size.x, cell_size.y) # Centered with cell size + rs.canvas_item_add_texture_rect(cell_instance, rect, cell_texture) + + var location_fixed = Vector2(location.x * cell_size.x, location.y * cell_size.y) + var trans = Transform2D(0, location_fixed) + rs.canvas_item_set_transform(cell_instance, trans) + + return cell_instance + +## Generate the world with the initial cells +func generate_world() -> void: + for x in range(world_size.x): + world.append([]) + for y in range(world_size.y): + if randi_range(0, 1): + world[x].append(create_cell(Vector2(x, y))) + else: + world[x].append(0) diff --git a/world.tscn b/world.tscn new file mode 100644 index 0000000..c03eaf7 --- /dev/null +++ b/world.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://d3twfk56sjf2m"] + +[ext_resource type="Script" path="res://world.gd" id="1_wavft"] +[ext_resource type="Texture2D" uid="uid://c2vm5pfsamed4" path="res://icon.svg" id="2_8r6bn"] + +[node name="World" type="Node2D"] +script = ExtResource("1_wavft") +world_seed = 123 +cell_texture = ExtResource("2_8r6bn") + +[node name="CanvasLayer" type="CanvasLayer" parent="."] + +[node name="Camera2D" type="Camera2D" parent="."] +position = Vector2(576, 320)