Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2776482c21 | |||
| d802c901ac | |||
| 74757358c6 | |||
| 2bc9e3175b |
@@ -0,0 +1,47 @@
|
||||
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
|
||||
self.rotate(-PI / 2 * facing_direction)
|
||||
|
||||
func collect(input_value: int):
|
||||
if empty:
|
||||
$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] = -14 + (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
|
||||
elif facing_direction == 1:
|
||||
next_pos[0] += 1
|
||||
elif facing_direction == 2:
|
||||
next_pos[1] -= 1
|
||||
elif facing_direction == 3:
|
||||
next_pos[0] -= 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 = -14.0
|
||||
offset_right = 14.0
|
||||
offset_bottom = 9.0
|
||||
theme_override_colors/font_color = Color(0.23529412, 0, 0.8862745, 1)
|
||||
text = "-99"
|
||||
horizontal_alignment = 1
|
||||
vertical_alignment = 1
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
extends Sprite2D
|
||||
|
||||
var producer_value: int
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
print("Placed Extractor at: (%s, %s)" % [self.global_position[0], self.global_position[1]])
|
||||
|
||||
func setup(setup_value):
|
||||
producer_value = setup_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)
|
||||
|
||||
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"]
|
||||
|
||||
+90
-30
@@ -14,6 +14,7 @@ const DELETE_SCENE = preload("res://delete.tscn")
|
||||
|
||||
enum Mode { NONE, PLACE_EXTRACTOR, PLACE_BELT, PLACE_ADDER, DELETE }
|
||||
var current_mode = Mode.NONE
|
||||
var current_direction = 0
|
||||
var preview_item: Node2D = null # Holds the visual for the mouse
|
||||
|
||||
# These should likely be changed to be the same format as above
|
||||
@@ -67,6 +68,14 @@ func _input(event):
|
||||
elif event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
|
||||
clear_preview()
|
||||
current_mode = Mode.NONE
|
||||
if event is InputEventKey and event.pressed:
|
||||
print('key pressed')
|
||||
if event.keycode == KEY_BRACKETLEFT:
|
||||
current_direction = (current_direction + 1) % 4
|
||||
preview_item.rotate(-PI / 2)
|
||||
elif event.keycode == KEY_BRACKETRIGHT:
|
||||
current_direction = (current_direction - 1) % 4
|
||||
preview_item.rotate(+PI / 2)
|
||||
|
||||
|
||||
# Returns the grid position of the mouse
|
||||
@@ -81,10 +90,10 @@ func initialize_world_center() -> void:
|
||||
for y in range(-1, 2):
|
||||
var current_coord = Vector2i(x, y)
|
||||
|
||||
grid_data[current_coord] = {
|
||||
grid_data[current_coord] = [{
|
||||
"type": "consumer",
|
||||
"is_center": (x == 0 and y == 0) # Only (0,0) is marked as the visual center
|
||||
}
|
||||
}]
|
||||
print("World Core Initialized: 3x3 Consumer registered from (-1,-1) to (1,1)")
|
||||
if producer_scene:
|
||||
print("Placing consumer at (0, 0)")
|
||||
@@ -135,20 +144,12 @@ func process_grid_data_at(coords: Vector2i) -> void:
|
||||
|
||||
# Formula: Start at 1, add 1 extra value for every 10 tiles away
|
||||
var producer_value = 1 + floor(distance / 10.0)
|
||||
|
||||
# Register the producer in our global data registry
|
||||
# disabled: for x in range(-1, 2):
|
||||
# disabled: for y in range(-1, 2):
|
||||
# disabled: grid_data[coords + Vector2i(x, y)] = {
|
||||
# disabled: "type": "producer",
|
||||
# disabled: "value": producer_value
|
||||
# disabled: }
|
||||
|
||||
|
||||
grid_data[coords] = {
|
||||
|
||||
grid_data[coords] = [{
|
||||
"type": "producer",
|
||||
"value": producer_value
|
||||
}
|
||||
}]
|
||||
|
||||
if producer_scene:
|
||||
print("Placing producer at " + str(coords.x) + ", " + str(coords.y))
|
||||
@@ -169,6 +170,7 @@ func set_preview_mode(mode_type: Mode, scene_to_preview: PackedScene):
|
||||
|
||||
if scene_to_preview:
|
||||
preview_item = scene_to_preview.instantiate()
|
||||
preview_item.rotate(-PI / 2 * current_direction)
|
||||
# Optional: Make the preview slightly transparent
|
||||
preview_item.modulate.a = 0.5
|
||||
add_child(preview_item)
|
||||
@@ -201,45 +203,56 @@ 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,
|
||||
{
|
||||
"type": new_item.name,
|
||||
})
|
||||
|
||||
# Custom setup needed
|
||||
if item == EXTRACTOR_SCENE:
|
||||
new_item.setup(object_at_coords['value'])
|
||||
elif item == BELT_SCENE:
|
||||
new_item.setup(spawn_pos, current_direction)
|
||||
else:
|
||||
print("No custom setup for %s" % item)
|
||||
|
||||
func delete_item(target_pos: Vector2):
|
||||
print("deleting item")
|
||||
var targets = get_tree().get_nodes_in_group("Placed_objects")
|
||||
func delete_item(target_pos: Vector2i):
|
||||
var targets = get_tree().get_nodes_in_group("placed_objects")
|
||||
for item in targets:
|
||||
if item.global_position == target_pos:
|
||||
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:
|
||||
print("deleting item")
|
||||
item.queue_free()
|
||||
delete_object_at_coordinate(target_pos)
|
||||
break # Prevents deleting multiple items.
|
||||
|
||||
# Helper function: Check if a coordinate is occupied by a building
|
||||
func get_object_at_coordinate(coords: Vector2i):
|
||||
if grid_data.has(coords):
|
||||
return grid_data[coords]
|
||||
print(grid_data)
|
||||
return grid_data[coords][-1] # Return the last item there
|
||||
return null # Returns null if it's just an empty background tile
|
||||
|
||||
# Helper function: Force place an object into the grid data registry
|
||||
func register_object_at_coordinate(coords: Vector2i, object_data: Dictionary) -> void:
|
||||
grid_data[coords] = object_data
|
||||
if grid_data.has(coords):
|
||||
grid_data[coords].append(object_data)
|
||||
else:
|
||||
grid_data[coords] = [object_data]
|
||||
|
||||
func delete_object_at_coordinate(coords: Vector2i):
|
||||
if grid_data.has(coords):
|
||||
grid_data[coords].pop_back()
|
||||
if grid_data[coords] == []:
|
||||
grid_data.erase(coords)
|
||||
|
||||
|
||||
# --- UI Button Signals ---
|
||||
func _on_extractor_button_pressed():
|
||||
set_preview_mode(Mode.PLACE_EXTRACTOR, EXTRACTOR_SCENE)
|
||||
##
|
||||
##func _on_belt_button_pressed():
|
||||
## set_preview_mode(Mode.PLACE_BELT, BELT_SCENE)
|
||||
##
|
||||
##func _on_delete_button_pressed():
|
||||
## clear_preview()
|
||||
## current_mode = Mode.DELETE
|
||||
##
|
||||
|
||||
|
||||
func _on_belt_button_pressed() -> void:
|
||||
set_preview_mode(Mode.PLACE_BELT, BELT_SCENE)
|
||||
@@ -247,6 +260,53 @@ func _on_belt_button_pressed() -> void:
|
||||
func _on_adder_button_pressed() -> void:
|
||||
set_preview_mode(Mode.PLACE_ADDER, ADDER_SCENE)
|
||||
|
||||
|
||||
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])
|
||||
|
||||
+4
-3
@@ -2,13 +2,14 @@ extends Sprite2D
|
||||
|
||||
var output_value: int
|
||||
|
||||
func setup(output_value):
|
||||
func setup(out_value):
|
||||
$tooltip.hide()
|
||||
print("Producer spawned physically with a value of: ", output_value)
|
||||
$text_value.text = str(output_value)
|
||||
print("Producer spawned physically with a value of: ", out_value)
|
||||
$text_value.text = str(out_value)
|
||||
$tooltip.text = "Placed at: (%s, %s)" % [self.global_position[0], self.global_position[1]]
|
||||
print($tooltip.size )
|
||||
$tooltip.size = Vector2i(100, 100)
|
||||
output_value = out_value
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
|
||||
Reference in New Issue
Block a user