summaryrefslogtreecommitdiff
path: root/Scripts
diff options
context:
space:
mode:
authoraltaf-creator <dev@altafcreator.com>2024-04-13 21:50:12 +0700
committeraltaf-creator <dev@altafcreator.com>2024-04-13 21:50:12 +0700
commitb97ebd793eb17032f3929f307dfe644b17714619 (patch)
tree03a558e77757112a6fddce45dbbcc0139df479c2 /Scripts
parentc5a806528af8a4de5f903d26cd33edb7e0bb6597 (diff)
bare bones ai, softlock issues
Diffstat (limited to 'Scripts')
-rw-r--r--Scripts/AI.gd37
-rw-r--r--Scripts/GameLoop.gd11
-rw-r--r--Scripts/Generator.gd43
-rw-r--r--Scripts/GlobalVariables.gd1
-rw-r--r--Scripts/ModifierLabel.gd9
-rw-r--r--Scripts/NumberArea.gd2
-rw-r--r--Scripts/NumberNode.gd39
-rw-r--r--Scripts/Player.gd6
8 files changed, 121 insertions, 27 deletions
diff --git a/Scripts/AI.gd b/Scripts/AI.gd
new file mode 100644
index 0000000..d5cbcbe
--- /dev/null
+++ b/Scripts/AI.gd
@@ -0,0 +1,37 @@
+extends Node
+class_name AI
+
+@export_group("Values")
+
+@export_group("Scenes")
+@export var shape_cast_original : ShapeCast2D
+@export var shape_cast_skill_issue: ShapeCast2D
+
+func _ready():
+ GlobalVariables.ai = self
+
+func step(alternative : bool = false):
+ var shape_cast : ShapeCast2D
+
+ if !alternative:
+ shape_cast = shape_cast_original
+ else:
+ shape_cast = shape_cast_skill_issue
+
+ shape_cast.force_shapecast_update()
+
+ var nodes : Array[NumberNode] = []
+
+ print(shape_cast.collision_result)
+
+ for collision in shape_cast.collision_result:
+ if collision.collider.get_node("..") is NumberNode:
+ if collision.collider.get_node("..").do_scanning:
+ nodes.append(collision.collider.get_node(".."))
+
+ nodes.sort_custom(func(a, b): return a.value > b.value)
+
+ if nodes.size() > 0:
+ nodes[0].join_from($"..")
+ else:
+ step(true)
diff --git a/Scripts/GameLoop.gd b/Scripts/GameLoop.gd
index ea5e157..04cd370 100644
--- a/Scripts/GameLoop.gd
+++ b/Scripts/GameLoop.gd
@@ -2,10 +2,19 @@ extends Node
class_name GameLoop
@export var turns : int
+@export var label : Label
+var current_turns : int = 0
+var prev_turn : int = -1
# Called when the node enters the scene tree for the first time.
func _ready():
GlobalVariables.gameloop = self
func _process(_delta):
- pass
+ if current_turns >= turns:
+ label.text = "FINISHED!"
+
+ if prev_turn != current_turns:
+ prev_turn = current_turns
+ label.text = "Turns Left: " + str(turns - current_turns)
+ GlobalVariables.ai.step()
diff --git a/Scripts/Generator.gd b/Scripts/Generator.gd
index c236f7c..834e07b 100644
--- a/Scripts/Generator.gd
+++ b/Scripts/Generator.gd
@@ -23,36 +23,54 @@ func generate():
var count : int = randi_range(min_nodes, max_nodes)
print(count)
+ var new_nodes : Array[NumberNode]
+
for i in count:
var is_big : bool = false
- if randf() < 0.1:
+ if randf() < 0.06:
is_big = true
var new : NumberNode = numbernode.instantiate()
if is_big:
- new.position = Vector2(randf_range(30, 1250), randf_range(30, 690))
+ new.position = Vector2(randf_range(400, 900), randf_range(200, 400)) # disgusting hardcoded values, or not really?
else:
- new.position = Vector2(randf_range(30, 1250), randf_range(30, 690)) # disgusting hardcoded values, or not really?
-
+ new.position = Vector2(randf_range(30, 1250), randf_range(30, 690))
+
get_node(GlobalVariables.NODE).add_child(new)
- var shape_cast : ShapeCast2D = new.get_node("ShapeCast2D")
-
- if !is_big: check_position_and_retry(new, shape_cast, 0)
+ var shape_cast : ShapeCast2D
+
+ if !is_big:
+ shape_cast = new.get_node("ShapeCast2D")
+ else:
+ new.get_node("NumberArea2D").set_collision_layer_value(1, false)
+ new.get_node("NumberArea2D").set_collision_layer_value(3, true)
+ shape_cast = new.get_node("ShapeCast2D2")
+
+ check_position_and_retry(new, shape_cast, 0)
new.value = randi_range(0, 10)
if is_big:
- new.value *= 20
+ new.value = new.value + 10 * 5
new.modifier_operation = clamp(abs(roundi(randfn(0, 10))) - 7, 0, 4)
-
+
if new.modifier_operation == NumberNode.Operations.DIVISION:
new.modifier = randi_range(1, 3)
else:
new.modifier = snapped(randf_range(-3, 3), 0.1)
-
+
+ if is_big:
+ new.scale = Vector2(1.4, 1.4)
+ new.get_node("NumberArea2D").set_collision_layer_value(1, true)
+ new.get_node("NumberArea2D").set_collision_layer_value(3, true)
+
new.update_visuals()
+ #new_nodes.append(new)
+
+ #for n in new_nodes:
+ # n.softlock_check()
func check_position_and_retry(node : NumberNode, shape_cast : ShapeCast2D, tries : int = 0):
shape_cast.force_shapecast_update()
@@ -60,9 +78,10 @@ func check_position_and_retry(node : NumberNode, shape_cast : ShapeCast2D, tries
var collision_count = shape_cast.get_collision_count()
print(node.name + " " + str(collision_count))
if collision_count > 1:
- node.queue_free() # TODO: failed? screw you! DIE!!!!
+ node.queue_free() # TODO: failed? screw you! DIE!!!! NO RETRIES FOR YOU, FAILURE
#tries += 1
-
+ tries = tries # compiler won't complain
+
#node.position = Vector2(randf_range(0, 1280), randf_range(0, 720)) # disgusting hardcoded values, or not really?
#shape_cast.position = Vector2.ZERO
#shape_cast.global_position = node.global_position
diff --git a/Scripts/GlobalVariables.gd b/Scripts/GlobalVariables.gd
index 7f8a305..fd7ce6c 100644
--- a/Scripts/GlobalVariables.gd
+++ b/Scripts/GlobalVariables.gd
@@ -6,4 +6,5 @@ const NODE = "/root/MainNode2D/"
var is_snapping : bool = false
## Player, please assign yourself here at _ready():
var player : Player
+var ai : AI
var gameloop : GameLoop
diff --git a/Scripts/ModifierLabel.gd b/Scripts/ModifierLabel.gd
new file mode 100644
index 0000000..1a2ed2e
--- /dev/null
+++ b/Scripts/ModifierLabel.gd
@@ -0,0 +1,9 @@
+extends Label
+
+func pop(txt : String):
+ visible = true
+ text = txt
+ $"Timer".start()
+
+func _on_timer_timeout():
+ visible = false
diff --git a/Scripts/NumberArea.gd b/Scripts/NumberArea.gd
index 443fada..78fc53f 100644
--- a/Scripts/NumberArea.gd
+++ b/Scripts/NumberArea.gd
@@ -9,7 +9,7 @@ func _on_mouse_entered():
if !parent.do_scanning:
return
- if GlobalVariables.is_snapping && GlobalVariables.player.legal:
+ if GlobalVariables.is_snapping && GlobalVariables.player.legal && GlobalVariables.gameloop.current_turns < GlobalVariables.gameloop.turns:
GlobalVariables.player.target = parent
func _on_mouse_exited():
diff --git a/Scripts/NumberNode.gd b/Scripts/NumberNode.gd
index 41c5d4a..b566781 100644
--- a/Scripts/NumberNode.gd
+++ b/Scripts/NumberNode.gd
@@ -18,7 +18,6 @@ enum Operations {
@export_group("Visuals")
@export_subgroup("Visual Nodes")
@export var label : Label
-@export var label_modifier : Label
@export var sprite : Sprite2D
@export var sprite_modifier : Sprite2D
@export_subgroup("Visual Values")
@@ -34,16 +33,22 @@ func _process(_delta):
func join_from(node : NumberNode):
node.value += self.value
-
+
+ var modifier_label = node.get_node_or_null("ModifierLabel")
+
if self.modifier_operation == Operations.ADDITION:
node.value += roundi(self.modifier)
+ if modifier_label: modifier_label.pop("+ " + str(self.modifier))
if self.modifier_operation == Operations.SUBTRACTION:
node.value -= roundi(self.modifier)
+ if modifier_label: modifier_label.pop("- " + str(self.modifier))
if self.modifier_operation == Operations.MULTIPLICATION:
node.value = roundi(float(node.value) * self.modifier)
+ if modifier_label: modifier_label.pop("* " + str(self.modifier))
if self.modifier_operation == Operations.DIVISION:
node.value = roundi(float(node.value) / float(self.modifier))
-
+ if modifier_label: modifier_label.pop("/ " + str(self.modifier))
+
node.position = self.position
node.update_visuals()
@@ -54,21 +59,31 @@ func update_visuals():
sprite.modulate = color
if self.modifier_operation == Operations.ADDITION:
- label_modifier.text = "+ " + str(self.modifier)
sprite_modifier.visible = true
- label_modifier.visible = true
if self.modifier_operation == Operations.SUBTRACTION:
- label_modifier.text = "- " + str(self.modifier)
sprite_modifier.visible = true
- label_modifier.visible = true
if self.modifier_operation == Operations.MULTIPLICATION:
- label_modifier.text = "× " + str(self.modifier)
sprite_modifier.visible = true
- label_modifier.visible = true
if self.modifier_operation == Operations.DIVISION:
- label_modifier.text = "÷ " + str(self.modifier)
sprite_modifier.visible = true
- label_modifier.visible = true
if self.modifier_operation == Operations.NONE:
sprite_modifier.visible = false
- label_modifier.visible = false
+
+## @deprecated
+func softlock_check():
+ var shape_cast : ShapeCast2D = $"ShapeCast2D3" # sorry, too lazy to change names!
+
+ shape_cast.force_shapecast_update()
+
+ if shape_cast.get_collision_count() < 2:
+ print("softlock may happen, " + str(shape_cast.collision_result))
+ var pos = Vector2(randf_range(global_position.x - 80, global_position.x + 80), randf_range(global_position.y - 80, global_position.y + 80))
+
+ var scene : PackedScene = load("res://Reusable Scenes/number_node.tscn")
+
+ var new = scene.instantiate()
+ new.global_position = pos
+ new.value = value
+
+ get_node(GlobalVariables.NODE).add_child(new)
+ new.softlock_check()
diff --git a/Scripts/Player.gd b/Scripts/Player.gd
index e422ba8..f3b9667 100644
--- a/Scripts/Player.gd
+++ b/Scripts/Player.gd
@@ -6,8 +6,11 @@ class_name Player
var target : NumberNode
var legal : bool
+var gameloop : GameLoop
+
func _ready():
GlobalVariables.player = self
+ gameloop = GlobalVariables.gameloop
func _input(event):
if GlobalVariables.is_snapping:
@@ -25,8 +28,9 @@ func _input(event):
if event is InputEventMouseButton && !event.pressed:
GlobalVariables.is_snapping = false
- if target != null:
+ if target != null && gameloop.current_turns < gameloop.turns:
target.join_from($"..")
+ gameloop.current_turns += 1
line.points[1] = Vector2.ZERO