working, mostly.
bendn 2022-03-03
parent 838260b · commit 793684c
-rw-r--r--InputHandler.tscn6
-rw-r--r--World.gd38
-rw-r--r--World.tscn14
-rw-r--r--dots/dot.gd30
-rw-r--r--dots/dot_template.tscn1
-rw-r--r--input_handler.gd44
-rw-r--r--input_handler_line.gd19
7 files changed, 109 insertions, 43 deletions
diff --git a/InputHandler.tscn b/InputHandler.tscn
index c01dce6..5113afc 100644
--- a/InputHandler.tscn
+++ b/InputHandler.tscn
@@ -1,6 +1,7 @@
-[gd_scene load_steps=2 format=2]
+[gd_scene load_steps=3 format=2]
[ext_resource path="res://input_handler.gd" type="Script" id=1]
+[ext_resource path="res://input_handler_line.gd" type="Script" id=2]
[node name="InputHandler" type="CanvasLayer"]
script = ExtResource( 1 )
@@ -10,3 +11,6 @@ default_color = Color( 0, 0, 0, 0 )
joint_mode = 2
end_cap_mode = 2
antialiased = true
+
+[node name="to_mouse_line" type="Node2D" parent="."]
+script = ExtResource( 2 )
diff --git a/World.gd b/World.gd
index bcc3a67..f80938a 100644
--- a/World.gd
+++ b/World.gd
@@ -9,35 +9,34 @@ var counted_done = 0
var h = GlobalVars.height
var w = GlobalVars.width
-
func _ready():
Events.connect("done_moving", self, "_on_dot_done_moving")
Events.connect("dots_done_moving", self, "_dots_done_moving")
Events.connect("turn_over", self, "_move_dots")
- Events.connect("level_initialized", self, "_move_dots")
- spawn_dots(Leveldata.dots)
+# Events.connect("level_initialized", self, "_move_dots")
+ spawn_dots()
-func spawn_dots(library: Array):
+func spawn_dots(library: Array = Leveldata.dots):
var lib_size = library.size()
for wi in w:
for hi in h:
var current_row_item
- var new_dot = choose_random_dot(lib_size, library)
- if GlobalVars.turns_used != 0: # if its the first turn, the arrays will be empty, and i dont want to pad them with " "
+ var new_dot : dot = choose_random_dot(lib_size, library)
+ if GlobalVars.turns_used != 0: # if its the first turn, the arrays will be empty, and i dont want to pad them with 0
current_row_item = EntityTracker.rows[hi][wi]
- if current_row_item != 0:
+ if typeof(current_row_item) != TYPE_INT:
continue
else:
EntityTracker.rows[hi].append(new_dot)
dots_holder.add_child(new_dot)
var offset = Vector2(GlobalVars.size.x * wi, GlobalVars.size.y * hi)
var new_pos = ($Corners/Position2D.global_position + offset).snapped(GlobalVars.size)
- new_dot.global_position = new_pos
+ new_dot._initial_move(new_pos)
+ EntityTracker.rows[hi][wi] = new_dot
Events.emit_signal("level_initialized")
-
-func choose_random_dot(lib_size, library) -> Array:
+func choose_random_dot(lib_size, library) -> Node2D:
randomize()
var rand_number = randi() % lib_size
var new_dot = library[rand_number].instance()
@@ -55,9 +54,20 @@ func _on_dot_done_moving():
func _dots_done_moving():
pass
-
func _move_dots():
- for row in EntityTracker._flip_rows():
+ for item in EntityTracker.rows:
+ print(item)
+ yield(get_tree(), "idle_frame")
+ counted_done = 0
+ for row in EntityTracker._flip_rows(): # go through the tree from the bottom up
+ for Dot in row:
+ if Dot is dot:
+ Dot.move(true) # move the collisions dow, and only the collisions
+ yield(get_tree(), "idle_frame")
+ spawn_dots() # spawn in the dots, once
+ for row in EntityTracker._flip_rows(): # same as above, but this time move the sprites too
for Dot in row:
- Dot.move()
- yield(get_tree(), "idle_frame")
+ if Dot is dot:
+ Dot.move()
+
+
diff --git a/World.tscn b/World.tscn
index dc59b79..663abe8 100644
--- a/World.tscn
+++ b/World.tscn
@@ -3,7 +3,7 @@
[ext_resource path="res://World.gd" type="Script" id=1]
[sub_resource type="RectangleShape2D" id=1]
-extents = Vector2( 350, 35 )
+extents = Vector2( 350, 17.5 )
[node name="World" type="Node2D"]
script = ExtResource( 1 )
@@ -11,17 +11,19 @@ script = ExtResource( 1 )
[node name="Corners" type="Node2D" parent="."]
[node name="Position2D" type="Position2D" parent="Corners"]
-position = Vector2( 70, 140 )
-
-[node name="Position2D3" type="Position2D" parent="Corners"]
-position = Vector2( 630, 770 )
+position = Vector2( 70, 280 )
[node name="Dots" type="Node2D" parent="."]
[node name="area" type="Area2D" parent="."]
-position = Vector2( 350, 840 )
+position = Vector2( 350, 824 )
collision_layer = 2
collision_mask = 0
+__meta__ = {
+"_edit_group_": true,
+"_edit_lock_": true
+}
[node name="CollisionShape2D" type="CollisionShape2D" parent="area"]
+position = Vector2( 0, -1.5 )
shape = SubResource( 1 )
diff --git a/dots/dot.gd b/dots/dot.gd
index 40c77be..6057185 100644
--- a/dots/dot.gd
+++ b/dots/dot.gd
@@ -7,21 +7,43 @@ onready var ray : RayCast2D = $dot_collisions/dotray
onready var dotsprite : Sprite = $dotsprite
var tween = Tween.new()
+var initial = true
+
+var prev_go_to
export(GlobalVars.type) var dot_type
func _ready():
add_child(tween)
-func move():
+func move(collision_only = false):
ray.force_raycast_update()
var go_to = Vector2(global_position.x, ray.get_collision_point().y - 35)
- dotcollisions.global_position = go_to
+ if collision_only:
+ dotcollisions.global_position = go_to
+ prev_go_to = go_to
+ return
+ else: # cuz the ray moves too you see
+ go_to = prev_go_to
+ tween_item_position(dotsprite, dotsprite.global_position, go_to, .25)
+ # log motion into entity tracker
+ for rows in EntityTracker.rows:
+ var i = rows.find(self)
+ if i != -1:
+ rows[i] = 0
+ yield(tween, "tween_all_completed")
+ Events.emit_signal("done_moving")
- tween.interpolate_property(dotsprite, "global_position",
- dotsprite.global_position, go_to, .25,
+func tween_item_position(node, old, new, length):
+ tween.interpolate_property(node, "global_position",
+ old, new, length,
Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
tween.start()
+
+func _initial_move(position):
+ var parameters = [self, Vector2(position.x, -200), position, 2]
+# global_position = parameters[1]
+ tween_item_position(parameters[0], parameters[1], parameters[2], parameters[3])
yield(tween, "tween_all_completed")
Events.emit_signal("done_moving")
diff --git a/dots/dot_template.tscn b/dots/dot_template.tscn
index d80d830..b23130b 100644
--- a/dots/dot_template.tscn
+++ b/dots/dot_template.tscn
@@ -17,6 +17,7 @@ collision_mask = 2
shape = SubResource( 1 )
[node name="dotray" type="RayCast2D" parent="dot_collisions"]
+visible = false
enabled = true
cast_to = Vector2( 0, 1000 )
collision_mask = 2
diff --git a/input_handler.gd b/input_handler.gd
index ccd2d3b..8f4941a 100644
--- a/input_handler.gd
+++ b/input_handler.gd
@@ -1,23 +1,31 @@
extends CanvasLayer
-var pressed = false setget set_pressed
+var pressed = false
var nodes_with_points = []
+var current_color : Color
+var current_point = Vector2()
onready var line = $Line2D
+func _ready():
+ $to_mouse_line._setup(self, line)
func _pressed(event: InputEvent, node: dot):
if not GlobalVars.state == GlobalVars.states.player_turn:
return
+ if not event is InputEventMouse:
+ return
var node_center = node.dotcollisions.global_position
if (
line.get_point_count() > 0
- and node.color == line.default_color
+ and node.color == current_color
and not node in nodes_with_points
+ and node.global_position.distance_to(current_point) == 70
):
- print("points", line.get_point_count())
+ print(node.global_position.distance_to(current_point))
line.add_point(node_center)
+
nodes_with_points.append(node)
elif event.is_action("click") and event.is_pressed() and pressed == false: # on first click
pressed = true
@@ -25,26 +33,26 @@ func _pressed(event: InputEvent, node: dot):
nodes_with_points.clear()
nodes_with_points.append(node)
line.add_point(node_center)
- line.add_point(line.get_global_mouse_position())
line.default_color = node.color
+ current_color = node.color
func _physics_process(_delta):
if line.get_point_count() > 0:
- if Input.is_action_pressed("click") and pressed == true:
- move_last_point_to_mouse()
- else:
+ current_point = line.get_point_position(line.get_point_count() - 1)
+ if not Input.is_action_pressed("click"):
+ # clear up the entity tracker
+ if line.get_point_count() > 2:
+ for node in nodes_with_points:
+ for height in EntityTracker.rows:
+ var i = height.find(node) # look for node in rows
+ if i != -1:
+ height[i] = 0
+ node.queue_free()
+ break
+ Events.emit_signal("turn_over")
+ GlobalVars.state = GlobalVars.states.level_turn
+ GlobalVars.turns_used += 1
line.clear_points()
nodes_with_points.clear()
pressed = false
-
-
-func set_pressed(new_pressed):
- pressed = new_pressed
- set_physics_process(pressed)
-
-
-func move_last_point_to_mouse():
- # get_point count counts from one, remove point counts from 0
- line.remove_point(line.get_point_count() - 1)
- line.add_point(line.get_global_mouse_position())
diff --git a/input_handler_line.gd b/input_handler_line.gd
new file mode 100644
index 0000000..531a78f
--- /dev/null
+++ b/input_handler_line.gd
@@ -0,0 +1,19 @@
+extends Node2D
+
+var parent
+var line
+
+func _ready():
+ set_process(false)
+
+func _draw():
+ if parent.pressed:
+ draw_line(parent.current_point, get_global_mouse_position(), get_parent().current_color, line.width, true)
+
+func _setup(_parent, _line):
+ line = _line
+ parent = _parent
+ set_process(true)
+
+func _process(_delta):
+ update()