initial click-drag functionality (unfinished)

This commit is contained in:
effie 2025-06-26 00:25:28 +10:00
parent 2c3fa47ac5
commit a7da10c17c
3 changed files with 207 additions and 46 deletions

View File

@ -2,44 +2,208 @@ extends Node3D
var load_tile = preload("res://tiles/base_tile/tile.tscn")
var tiledict = {}
var tile_drag_dict = {}
var tile_drag_array_global = []
var tile_count_x_hist = null
var tile_count_z_hist = null
var tile_drag_x_hist = null
var tile_drag_z_hist = null
func _ready():
pass
func init_tile(float_build_start_pos, float_build_mouse_pos):
func clear_build_drag():
tile_count_x_hist = 0
tile_count_z_hist = 0
tile_drag_x_hist = 0
tile_drag_z_hist = 0
for i in tile_drag_dict:
var tile = tile_drag_dict[i]
tile.queue_free()
tile_drag_dict.clear()
func draw_tile_click(start_pos):
var build_start_pos: Vector3i = start_pos.snapped(Vector3i(1, 1, 1))
tile_drag_x_hist = 0
tile_drag_z_hist = 0
var tile = load_tile.instantiate()
tile.position = build_start_pos
tile_drag_dict[build_start_pos] = tile
add_child(tile)
func init_tile_drag(float_build_start_pos, float_build_mouse_pos):
var build_start_pos: Vector3i = float_build_start_pos.snapped(Vector3i(1.0, 1.0, 1.0))
var build_mouse_pos: Vector3i = float_build_mouse_pos.snapped(Vector3i(1.0, 1.0, 1.0))
var tile_count_x = build_mouse_pos.x - build_start_pos.x
var tile_count_z = build_mouse_pos.z - build_start_pos.z
if not tile_count_x_hist == tile_count_x or not tile_count_z_hist == tile_count_z:
var tile_count_x_diff = tile_count_x_hist - tile_count_x
var tile_count_z_diff = tile_count_z_hist - tile_count_z
tile_count_x_hist = tile_count_x
tile_count_z_hist = tile_count_z
draw_tile_drag(tile_count_x_diff, tile_count_z_diff, build_start_pos)
func draw_tile_drag(x_diff, z_diff, start_pos):
tile_drag_x_hist = x_diff + tile_drag_x_hist
tile_drag_z_hist = z_diff + tile_drag_z_hist
var flat_pos = build_start_pos + build_mouse_pos
var distance = abs(build_start_pos - build_mouse_pos)
if x_diff:
for z in range(min(0, tile_drag_z_hist), max(0, tile_drag_z_hist) + 1):
for x in abs(x_diff):
var id = Vector3i(start_pos.x - tile_drag_x_hist, 0, start_pos.z - tile_drag_z_hist - z)
if tile_drag_dict.has(id):
var tile = tile_drag_dict[id]
tile.queue_free()
tile_drag_dict.erase(id)
else:
var tile = load_tile.instantiate()
tile.position = id
tile_drag_dict[id] = tile
add_child(tile)
if z_diff:
for x in range(min(0, tile_drag_x_hist), max(0, tile_drag_x_hist) + 1):
for z in abs(z_diff):
var id = Vector3i(start_pos.x - tile_drag_x_hist - x, 0, start_pos.z - tile_drag_z_hist)
if tile_drag_dict.has(id):
var tile = tile_drag_dict[id]
tile.queue_free()
tile_drag_dict.erase(id)
else:
var tile = load_tile.instantiate()
tile.position = id
tile_drag_dict[id] = tile
add_child(tile)
print(tile_drag_dict)
#if x_diff > 0:
#var tile = load_tile.instantiate()
#tile.position = Vector3i(start_pos.x - tile_drag_x_hist, 0, tile_drag_z_hist - z)
#
#print(Vector3i(start_pos.x - tile_drag_x_hist, 0, tile_drag_z_hist - z))
#tile_drag_dict[tile.position] = tile
#
#add_child(tile)
#
#if x_diff < 0:
#print(Vector3i(start_pos.x - tile_drag_x_hist, 0, tile_drag_z_hist - z))
#var build_mouse_x = floor(build_mouse_pos.x)
#var build_mouse_z = floor(build_mouse_pos.z)
#print(tile_drag_z_hist)
# order of events:
# get start and current mouse pos
# check for changes in drag grid dimension
# if changes, send to gird draw func
# add/remove tiles along each changed dimension
#var tile_x_diff = null
#var tile_z_diff = null
#
#
#var grid_x_count = max(1, build_mouse_pos.x - build_start_pos.x)
#var grid_z_count = max(1, build_mouse_pos.z - build_start_pos.z)
#
#if tile_count_x_hist == null:
#tile_x_diff = 0
#else:
#tile_x_diff = tile_count_x_hist - tile_count_x
#
#tile_count_x_hist = tile_x_diff
##print(tile_count_x_hist)
#
#if not tile_count_z_hist:
#tile_z_diff = 0
#else:
#tile_z_diff = tile_count_z_hist - tile_count_z
#
#if tile_x_diff or tile_z_diff:
#draw_tile_drag(tile_x_diff, tile_z_diff, build_start_pos)
#tile_count_x_hist = tile_count_x
#tile_count_z_hist = tile_count_z
var crop_x = max(1, distance.x)
var crop_z = max(1, distance.z)
for x in crop_x:
for z in crop_z:
var id = flat_pos
var tile = load_tile.instantiate()
tile.position = build_start_pos + Vector3i(x, 0, z)
print("global", build_start_pos + Vector3i(x, 0, z))
if not tiledict.has(id):
#var tile_drag_array = []
#var tile_count_x_array = [0]
#var tile_count_z_array = [0]
#
#var tile_count_x = build_mouse_pos.x - build_start_pos.x
#var tile_count_z = build_mouse_pos.z - build_start_pos.z
#if tile_count_x < 0:
#tile_count_x_array.append_array(range(-1, tile_count_x - 1, -1))
#tile_count_x_array.reverse()
#
#if tile_count_x > 0:
#tile_count_x_array.append_array(range(1, tile_count_x + 1))
#
#if tile_count_z < 0:
#tile_count_z_array.append_array(range(-1, tile_count_z - 1, -1))
#tile_count_z_array.reverse()
#
#if tile_count_z > 0:
#tile_count_z_array.append_array(range(1, tile_count_z + 1))
#for x in range(min(0, tile_count_x), max(0, tile_count_x) + 1):
#for z in range(min(0, tile_count_z), max(0, tile_count_z) + 1):
#var tile_drag_pos = build_start_pos + Vector3i(x, 0, z)
#
#tile_drag_array.append(tile_drag_pos)
#for x in tile_count_x_array:
#for z in tile_count_z_array:
#
#print(tile_drag_array)
print("global after", build_start_pos + Vector3i(x, 0, z))
print("x (global)", id)
print("global", tile.position)
tiledict[id] = tile
add_child(tile)
#func draw_tile_drag(x_diff, z_diff, start_pos):
#print(x_diff, " ", z_diff)
#i
#
#tile.position = i
#
#tile_drag_dict[i] = tile
pass
#print(x_diff, " ", z_diff)
#for i in self.get_children():
#i.queue_free()
#for i in array:
#
#var id = i
#
#if not tile_drag_dict.has(id):
#var tile = load_tile.instantiate()
#
#tile.position = i
#
#tile_drag_dict[id] = tile
#
#add_child(tile)

View File

@ -10,31 +10,27 @@ var build_start_pos = null
func _ready():
add_child(map)
func _input(event):
pass
func _build_toggled(toggled_on) -> void:
if toggled_on:
build_enabled = true
else:
build_enabled = false
build_enabled = toggled_on
func toggle_building(event):
if event.pressed:
is_building = true
if not event.pressed:
is_building = false
is_building = event.pressed
func _on_area_3d_input_event(_camera, event, event_position, _normal, _shade_id):
if event.is_action("select") && build_enabled:
is_building = true
if event.is_action_pressed("select") && build_enabled:
build_start_pos = event_position
toggle_building(event)
if not event.is_action_pressed("select") && is_building:
map.draw_tile_click(build_start_pos)
if event.button_mask && is_building:
var build_mouse_pos = event_position
map.init_tile(build_start_pos, build_mouse_pos)
pass # Replace with function body.
map.init_tile_drag(build_start_pos, build_mouse_pos)
if not event.button_mask:
map.clear_build_drag()
pass

View File

@ -4,6 +4,7 @@
[ext_resource type="PlaneMesh" uid="uid://bis4hdushjnjm" path="res://tiles/base_tile/base_floor.tres" id="2_ipr02"]
[node name="Tile" type="Node3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.5, 0, 0.5)
script = ExtResource("1_v7x5k")
[node name="FloorMesh" type="MeshInstance3D" parent="."]