Basic belts working
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
extends Sprite2D
|
||||
|
||||
var empty: bool = true
|
||||
var value: int
|
||||
var move_timer: int = 0
|
||||
var map_position: Vector2
|
||||
var facing_direction: int
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func setup(pos, dir):
|
||||
map_position = pos
|
||||
facing_direction = dir
|
||||
|
||||
func collect(input_value: int):
|
||||
$value_text.text = str(input_value)
|
||||
$value_text.show()
|
||||
empty = false
|
||||
value = input_value
|
||||
|
||||
func move():
|
||||
#print("Moving item")
|
||||
move_timer += 1
|
||||
$value_text.position[1] = -30 + (move_timer * 2)
|
||||
if move_timer > 15:
|
||||
move_timer = 0
|
||||
empty = true
|
||||
$value_text.hide()
|
||||
var next_pos = map_position
|
||||
if facing_direction == 0:
|
||||
next_pos[1] += 1
|
||||
return [next_pos, value]
|
||||
return
|
||||
@@ -0,0 +1 @@
|
||||
uid://bh7ochydpenns
|
||||
@@ -1,6 +1,20 @@
|
||||
[gd_scene format=3 uid="uid://kw8ughroalov"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://m2en3pg3og4u" path="res://assets/images/belt.png" id="1_susb0"]
|
||||
[ext_resource type="Script" uid="uid://bh7ochydpenns" path="res://belt.gd" id="2_rfspg"]
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" unique_id=1338317761]
|
||||
texture = ExtResource("1_susb0")
|
||||
script = ExtResource("2_rfspg")
|
||||
|
||||
[node name="value_text" type="Label" parent="." unique_id=1850839196]
|
||||
visible = false
|
||||
z_index = 1
|
||||
offset_left = -14.0
|
||||
offset_top = -30.0
|
||||
offset_right = 14.0
|
||||
offset_bottom = -7.0
|
||||
theme_override_colors/font_color = Color(0.23529412, 0, 0.8862745, 1)
|
||||
text = "-99"
|
||||
horizontal_alignment = 1
|
||||
vertical_alignment = 1
|
||||
|
||||
+6
-5
@@ -6,14 +6,15 @@ var producer_value: int
|
||||
func _ready() -> void:
|
||||
print("Placed Extractor at: (%s, %s)" % [self.global_position[0], self.global_position[1]])
|
||||
|
||||
func setup(producer_value):
|
||||
producer_value = producer_value
|
||||
func setup(value):
|
||||
producer_value = value
|
||||
print("extractor value set as %s" % producer_value)
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func value() -> int:
|
||||
print("extractor value is %s" % producer_value)
|
||||
|
||||
func _on_timer_timeout() -> void:
|
||||
print("Extractor producing %s" % producer_value)
|
||||
$Timer.start()
|
||||
return producer_value
|
||||
|
||||
+1
-1
@@ -3,6 +3,6 @@
|
||||
[ext_resource type="Texture2D" uid="uid://lq8htcre3uve" path="res://assets/images/extractor.png" id="1_e3dte"]
|
||||
[ext_resource type="Script" uid="uid://dwe2kdx6e4rni" path="res://extractor.gd" id="2_x82y0"]
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" unique_id=1172832761]
|
||||
[node name="Extractor" type="Sprite2D" unique_id=1172832761]
|
||||
texture = ExtResource("1_e3dte")
|
||||
script = ExtResource("2_x82y0")
|
||||
|
||||
@@ -73,7 +73,17 @@ layout_mode = 2
|
||||
texture_normal = ExtResource("9_j5hk1")
|
||||
stretch_mode = 4
|
||||
|
||||
[node name="extractor_timer" type="Timer" parent="." unique_id=2112149298]
|
||||
wait_time = 5.0
|
||||
autostart = true
|
||||
|
||||
[node name="belt_timer" type="Timer" parent="." unique_id=1538161035]
|
||||
wait_time = 0.2
|
||||
autostart = true
|
||||
|
||||
[connection signal="pressed" from="CanvasLayer/Control/VBoxContainer/extractor_button" to="." method="_on_extractor_button_pressed"]
|
||||
[connection signal="pressed" from="CanvasLayer/Control/VBoxContainer/belt_button" to="." method="_on_belt_button_pressed"]
|
||||
[connection signal="pressed" from="CanvasLayer/Control/VBoxContainer/adder_button" to="." method="_on_adder_button_pressed"]
|
||||
[connection signal="pressed" from="CanvasLayer/Control/VBoxContainer/delete_button" to="." method="_on_delete_button_pressed"]
|
||||
[connection signal="timeout" from="extractor_timer" to="." method="_on_extractor_timer_timeout"]
|
||||
[connection signal="timeout" from="belt_timer" to="." method="_on_belt_timer_timeout"]
|
||||
|
||||
+52
-2
@@ -201,7 +201,7 @@ func place_item(item: PackedScene):
|
||||
print("Placing object")
|
||||
var new_item = item.instantiate()
|
||||
new_item.global_position = tile_layer.map_to_local(spawn_pos)
|
||||
new_item.add_to_group("Placed_objects")
|
||||
new_item.add_to_group("placed_objects")
|
||||
add_child(new_item)
|
||||
register_object_at_coordinate(
|
||||
spawn_pos,
|
||||
@@ -212,11 +212,13 @@ func place_item(item: PackedScene):
|
||||
# Custom setup needed
|
||||
if item == EXTRACTOR_SCENE:
|
||||
new_item.setup(object_at_coords['value'])
|
||||
elif item == BELT_SCENE:
|
||||
new_item.setup(spawn_pos, 0)
|
||||
else:
|
||||
print("No custom setup for %s" % item)
|
||||
|
||||
func delete_item(target_pos: Vector2i):
|
||||
var targets = get_tree().get_nodes_in_group("Placed_objects")
|
||||
var targets = get_tree().get_nodes_in_group("placed_objects")
|
||||
for item in targets:
|
||||
print("Trying to match delete at: " + str(tile_layer.local_to_map(item.global_position)[0]) + ", " + str(tile_layer.local_to_map(item.global_position)[1]))
|
||||
if tile_layer.local_to_map(item.global_position) == target_pos:
|
||||
@@ -255,3 +257,51 @@ func _on_adder_button_pressed() -> void:
|
||||
|
||||
func _on_delete_button_pressed() -> void:
|
||||
set_preview_mode(Mode.DELETE, DELETE_SCENE)
|
||||
|
||||
# Run the extractor update
|
||||
func _on_extractor_timer_timeout() -> void:
|
||||
var targets = get_tree().get_nodes_in_group("placed_objects")
|
||||
var production_coords: Array
|
||||
for target in targets:
|
||||
# Check if we are an extractor:
|
||||
if target.scene_file_path == 'res://extractor.tscn':
|
||||
position = tile_layer.local_to_map(target.global_position)
|
||||
print("Extractor produced a %s at %s, %s" % [target.value(), position[0], position[1]])
|
||||
# Make a list of positions where we need to try to produce:
|
||||
for x in range(-1, 2):
|
||||
for y in range(-1, 2):
|
||||
production_coords.append([Vector2(position[0] + x, position[1] + y), target.value()])
|
||||
|
||||
# Now loop through and see if there are empty conveyer belts in the correct positions
|
||||
for target in targets:
|
||||
# Check if this is a belt:
|
||||
if target.scene_file_path == 'res://belt.tscn' and target.empty:
|
||||
position = tile_layer.local_to_map(target.global_position)
|
||||
for prod in production_coords:
|
||||
if position == prod[0]:
|
||||
print("Extractor produced %s onto a belt!" %prod[1])
|
||||
target.collect(prod[1])
|
||||
|
||||
|
||||
func _on_belt_timer_timeout() -> void:
|
||||
#print('belt tick')
|
||||
# Gather the belts:
|
||||
var targets = get_tree().get_nodes_in_group("placed_objects")
|
||||
var next_belt_coords: Array
|
||||
var belts: Array
|
||||
for target in targets:
|
||||
# Check if this is a belt:
|
||||
if target.scene_file_path == 'res://belt.tscn':
|
||||
belts.append(target)
|
||||
if !target.empty:
|
||||
var result = target.move()
|
||||
if result != null:
|
||||
# Try to move the number to the next belt
|
||||
print("value fell off of belt")
|
||||
next_belt_coords.append(result)
|
||||
for belt in belts:
|
||||
for itm in next_belt_coords:
|
||||
position = tile_layer.local_to_map(belt.global_position)
|
||||
print("(%s, %s), (%s, %s)" %[position[0], position[1], itm[0][0], itm[0][1]])
|
||||
if position == itm[0]:
|
||||
belt.collect(itm[1])
|
||||
|
||||
Reference in New Issue
Block a user