commit 8b641f28765ee015dea42957c7625f70ecf32b2a
parent dcd005ec26a7f16b181c19b0eabbffd5d1fb9b35
Author: bsandro <email@bsandro.tech>
Date: Fri, 23 Aug 2024 01:38:22 +0300
Cell scaling support (with dynamic field size)
Diffstat:
3 files changed, 26 insertions(+), 18 deletions(-)
diff --git a/scenes/Cell.tscn b/scenes/Cell.tscn
@@ -4,6 +4,7 @@
[ext_resource type="Script" path="res://scripts/Cell.gd" id="2_uvr0w"]
[node name="Cell" type="Sprite2D"]
+scale = Vector2(0.2, 0.2)
texture = ExtResource("1_k1vbj")
centered = false
script = ExtResource("2_uvr0w")
diff --git a/scenes/main.tscn b/scenes/main.tscn
@@ -9,6 +9,6 @@ cell_scene = ExtResource("2_tjoxl")
tick_timer = NodePath("TickTimer")
[node name="TickTimer" type="Timer" parent="."]
-wait_time = 0.2
+wait_time = 0.1
[connection signal="timeout" from="TickTimer" to="." method="_on_tick"]
diff --git a/scripts/main.gd b/scripts/main.gd
@@ -1,9 +1,9 @@
extends Node
# calculated for 1280x720 window
-const FIELD_W = 25
-const FIELD_H = 14
+var field_w:int
+var field_h:int
var cell_w:int
var cell_h:int
var field:Array[Array]
@@ -11,22 +11,29 @@ var field:Array[Array]
@export var tick_timer:Timer
func _init():
- field.resize(FIELD_H)
- for y in range(FIELD_H):
- field[y].resize(FIELD_W)
- field[y].fill(0)
+ pass
# Called when the node enters the scene tree for the first time.
func _ready():
- randomize_field()
-
var cell = cell_scene.instantiate()
cell.position = Vector2(0, 0)
add_child(cell)
- cell_w = cell.texture.get_width()
- cell_h = cell.texture.get_height()
+ cell_w = cell.texture.get_width() * cell.transform.get_scale().x
+ cell_h = cell.texture.get_height() * cell.transform.get_scale().y
print("cell size: ", cell_w, "x", cell_h)
+
+ field_w = get_viewport().size.x / cell_w
+ field_h = get_viewport().size.y / cell_h
+
+ print("field size: ", field_w, "x", field_h)
+
+ field.resize(field_w)
+ for y in range(field_h):
+ field[y].resize(field_w)
+ field[y].fill(0)
+
+ randomize_field()
tick_timer.start()
# Called every frame. 'delta' is the elapsed time since the previous frame.
@@ -34,8 +41,8 @@ func _process(_delta):
pass
func randomize_field():
- for y in range(FIELD_H):
- for x in range(FIELD_W):
+ for y in range(field_h):
+ for x in range(field_w):
field[y][x] = randi_range(0, 1)
func clear_cells():
@@ -45,8 +52,8 @@ func clear_cells():
remove_child(child)
func render_field():
- for y in range(FIELD_H):
- for x in range(FIELD_W):
+ for y in range(field_h):
+ for x in range(field_w):
if field[y][x] == 1:
var cell = cell_scene.instantiate()
cell.position = Vector2(x*cell_w, y*cell_h)
@@ -54,8 +61,8 @@ func render_field():
func tick_field():
var field_new:Array[Array] = field.duplicate(true)
- for y in range(FIELD_H):
- for x in range(FIELD_W):
+ for y in range(field_h):
+ for x in range(field_w):
field_new[y][x] = tick_cell(x, y)
field = field_new
@@ -76,7 +83,7 @@ func sum_area(x:int, y:int):
var cnt:int = 0;
for xx in range(x-1, x+2):
for yy in range(y-1, y+2):
- if xx>=0 && yy>=0 && xx<FIELD_W && yy<FIELD_H:
+ if xx>=0 && yy>=0 && xx<field_w && yy<field_h:
cnt += field[yy][xx];
return cnt