basic tile placement, TODO: check valid placement with color, turn based placement
This commit is contained in:
parent
2a8bd73d07
commit
cabc35394f
97 changed files with 1348 additions and 948 deletions
|
|
@ -1,7 +1,6 @@
|
|||
extends Node3D
|
||||
|
||||
@onready var hex = $Hexagon
|
||||
@onready var coord_label = $Hexagon/Label3D
|
||||
@onready var placement_visualizer = $PlacementVisualizer
|
||||
|
||||
const DIR_N: Vector3 = Vector3(0, 1, -1)
|
||||
const DIR_NE: Vector3 = Vector3(1, 0, -1)
|
||||
|
|
@ -15,8 +14,12 @@ const DIR_ALL: Array[Vector3] = [DIR_N, DIR_NE, DIR_SE, DIR_S, DIR_SW, DIR_NW]
|
|||
|
||||
const size: float = 0.5
|
||||
|
||||
var used_cells: Dictionary = {}
|
||||
|
||||
@export var layer_height: float = 0.4
|
||||
|
||||
|
||||
|
||||
class CubeCoordinates:
|
||||
var q: float
|
||||
var r: float
|
||||
|
|
@ -53,6 +56,8 @@ func flat_hex_to_world_position(coords: AxialCoordinates) -> Vector2:
|
|||
#
|
||||
# return
|
||||
|
||||
const INSECT_TILE = preload("res://InsectTiles/InsectTile.tscn")
|
||||
|
||||
func world_to_hex_tile(coords: Vector2) -> AxialCoordinates:
|
||||
var q = (2.0/3.0 * coords.x) / size
|
||||
var r = (-1.0/3.0 * coords.x + sqrt(3.0)/3.0 * coords.y) / size
|
||||
|
|
@ -96,50 +101,182 @@ func cube_round(coords: CubeCoordinates) -> CubeCoordinates:
|
|||
func get_3d_pos(position2D: Vector2):
|
||||
return Plane(dragging_intersect_plane_normal, dragging_intersect_plane_distance).intersects_ray(get_viewport().get_camera_3d().project_ray_origin(position2D), get_viewport().get_camera_3d().project_ray_normal(position2D))
|
||||
|
||||
func _ready() -> void:
|
||||
pass
|
||||
#for x in range(-0, 1):
|
||||
# for y in range(-0, 10):
|
||||
# var hex_pos = flat_hex_to_world_position(AxialCoordinates.new(x, y))
|
||||
# var new_hex = hex.duplicate()
|
||||
# new_hex.position = Vector3(hex_pos.x, 0.0, hex_pos.y)
|
||||
# var hex_id = world_to_hex_tile(Vector2(hex_pos.x, hex_pos.y))
|
||||
# new_hex.get_node("Label3D").text = "%d, %d" % [hex_id.q, hex_id.r]
|
||||
# add_child(new_hex)
|
||||
var placements: Dictionary = {}
|
||||
|
||||
func spawn_random_tile() -> void:
|
||||
var tile_copy = hex.duplicate()
|
||||
var hex_pos = flat_hex_to_world_position(AxialCoordinates.new(randi_range(-20, 20), randi_range(-20, 20)))
|
||||
func get_neighbours(coords: Vector2i) -> Array[Vector2i]:
|
||||
return [
|
||||
Vector2i(coords.x + 1, coords.y), Vector2i(coords.x + 1, coords.y - 1), Vector2i(coords.x, coords.y - 1),
|
||||
Vector2i(coords.x - 1, coords.y), Vector2i(coords.x - 1, coords.y + 1), Vector2i(coords.x, coords.y + 1)
|
||||
]
|
||||
|
||||
var current_tile: Node3D
|
||||
|
||||
const HEX_OUTLINE = preload("res://hex_outline.tscn")
|
||||
|
||||
func _on_insect_selected(insect_resource: TileResource, is_black: bool) -> void:
|
||||
# create a hexagon with insect resource data
|
||||
#var tile = INSECT_TILE.instantiate()
|
||||
#tile.resource = insect_resource
|
||||
#tile.is_black = is_black
|
||||
#current_tile = tile
|
||||
#add_child(tile)
|
||||
|
||||
# spawn possible placement locations :)
|
||||
if used_cells.size() == 0: # we have no cells placed, display a placement outline at 0, 0
|
||||
var outline = HEX_OUTLINE.instantiate()
|
||||
var hex_pos = flat_hex_to_world_position(AxialCoordinates.new(0, 0))
|
||||
outline.position = Vector3(hex_pos.x, 0.0, hex_pos.y)
|
||||
outline.hex_pos = Vector2i(0, 0)
|
||||
outline.visible = true
|
||||
outline.insect_resource = insect_resource
|
||||
outline.is_black = is_black
|
||||
placement_visualizer.add_child(outline)
|
||||
placements[hex_pos] = outline
|
||||
elif used_cells.size() == 1: # we have ONE cell placed, this is a special case in which
|
||||
# the opposing player is allowed to place a tile that touches the enemy color
|
||||
# We display outline placement around all spaces of this single cell
|
||||
var single_cell = used_cells.keys().front()
|
||||
var neighbours = get_neighbours(single_cell)
|
||||
for neighbour in neighbours:
|
||||
var outline = HEX_OUTLINE.instantiate()
|
||||
var hex_pos = flat_hex_to_world_position(AxialCoordinates.new(neighbour.x, neighbour.y))
|
||||
outline.position = Vector3(hex_pos.x, 0.0, hex_pos.y)
|
||||
outline.hex_pos = neighbour
|
||||
outline.visible = true
|
||||
outline.insect_resource = insect_resource
|
||||
outline.is_black = is_black
|
||||
placement_visualizer.add_child(outline)
|
||||
placements[hex_pos] = outline
|
||||
else:
|
||||
# iterate over all used_cells, get all empty cells surrounding those cells
|
||||
# iterate over all those empty cells, check if they only neighbour the same color
|
||||
var possible_placements: Dictionary = {}
|
||||
|
||||
for hex in used_cells.keys():
|
||||
var neighbours = [
|
||||
Vector2i(hex.x + 1, hex.y), Vector2i(hex.x + 1, hex.y - 1), Vector2i(hex.x, hex.y - 1),
|
||||
Vector2i(hex.x - 1, hex.y), Vector2i(hex.x - 1, hex.y + 1), Vector2i(hex.x, hex.y + 1)
|
||||
]
|
||||
#var eligible: bool = true
|
||||
for neighbour in neighbours:
|
||||
if not used_cells.has(neighbour):
|
||||
possible_placements[neighbour] = true
|
||||
|
||||
for p in possible_placements:
|
||||
var neighbours = [
|
||||
Vector2i(p.x + 1, p.y), Vector2i(p.x + 1, p.y - 1), Vector2i(p.x, p.y - 1),
|
||||
Vector2i(p.x - 1, p.y), Vector2i(p.x - 1, p.y + 1), Vector2i(p.x, p.y + 1)
|
||||
]
|
||||
|
||||
var eligible: bool = true
|
||||
|
||||
for neighbour in neighbours:
|
||||
|
||||
if not used_cells.has(neighbour):
|
||||
continue
|
||||
|
||||
#if used_cells[neighbour].is_black != is_black:
|
||||
# eligible = false
|
||||
# break
|
||||
|
||||
if eligible:
|
||||
var outline = HEX_OUTLINE.instantiate()
|
||||
var hex_pos = flat_hex_to_world_position(AxialCoordinates.new(p.x, p.y))
|
||||
outline.position = Vector3(hex_pos.x, 0.0, hex_pos.y)
|
||||
outline.hex_pos = p
|
||||
outline.visible = true
|
||||
outline.insect_resource = insect_resource
|
||||
outline.is_black = is_black
|
||||
placement_visualizer.add_child(outline)
|
||||
placements[p] = outline
|
||||
|
||||
pass
|
||||
|
||||
pass
|
||||
|
||||
func _on_insect_placement_cancelled() -> void:
|
||||
if current_tile:
|
||||
current_tile.queue_free()
|
||||
current_tile = null
|
||||
|
||||
for child in placement_visualizer.get_children():
|
||||
child.queue_free()
|
||||
|
||||
func _on_insect_placed(resource: TileResource, is_black: bool, pos: Vector2i) -> void:
|
||||
var tile_copy = INSECT_TILE.instantiate()
|
||||
var hex_pos = flat_hex_to_world_position(AxialCoordinates.new(pos.x, pos.y))
|
||||
|
||||
tile_copy.position = Vector3(hex_pos.x, 20.0, hex_pos.y)
|
||||
tile_copy.resource = resource
|
||||
tile_copy.is_black = is_black
|
||||
var target_pos = Vector3(hex_pos.x, 0.0, hex_pos.y)
|
||||
|
||||
used_cells[Vector2i(pos.x, pos.y)] = tile_copy
|
||||
|
||||
add_child(tile_copy)
|
||||
|
||||
var tween = get_tree().create_tween()
|
||||
tween.tween_property(tile_copy, "position", target_pos, 1.0).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_EXPO)
|
||||
|
||||
func move_tile_to_random_position() -> void:
|
||||
var new_hex_pos = flat_hex_to_world_position(AxialCoordinates.new(randi_range(-20, 20), randi_range(-20, 20)))
|
||||
var sky_new_hex_pos = Vector3(new_hex_pos.x, 20.0, new_hex_pos.y)
|
||||
var ground_new_hex_pos = Vector3(new_hex_pos.x, 0.0, new_hex_pos.y)
|
||||
|
||||
var current_hex_pos = hex.position
|
||||
var sky_current_hex_pos = hex.position + Vector3(0.0, 20.0, 0.0)
|
||||
|
||||
var tween = get_tree().create_tween()
|
||||
tween.tween_property(hex, "position", sky_current_hex_pos, 0.5).set_ease(Tween.EASE_IN).set_trans(Tween.TRANS_EXPO)
|
||||
tween.tween_property(hex, "position", sky_new_hex_pos, 0.0)
|
||||
tween.tween_property(hex, "position", ground_new_hex_pos, 1.0).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_EXPO)
|
||||
func _ready() -> void:
|
||||
GameEvents.insect_selected.connect(_on_insect_selected)
|
||||
GameEvents.insect_placement_cancelled.connect(_on_insect_placement_cancelled)
|
||||
GameEvents.insect_placed.connect(_on_insect_placed)
|
||||
|
||||
return
|
||||
|
||||
for x in range(-6, 5):
|
||||
for y in range(-6, 5):
|
||||
var hex_pos = flat_hex_to_world_position(AxialCoordinates.new(x, y))
|
||||
|
||||
if randi_range(0, 1) == 0:
|
||||
var new_hex = INSECT_TILE.instantiate()
|
||||
new_hex.resource = preload("res://Tile/Prefabs/Bee.tres")
|
||||
new_hex.is_black = false
|
||||
new_hex.position = Vector3(hex_pos.x, 0.0, hex_pos.y)
|
||||
var hex_id = world_to_hex_tile(Vector2(hex_pos.x, hex_pos.y))
|
||||
add_child(new_hex)
|
||||
used_cells[Vector2i(x, y)] = new_hex
|
||||
else:
|
||||
continue
|
||||
|
||||
|
||||
#func spawn_random_tile() -> void:
|
||||
#var tile_copy = hex.duplicate()
|
||||
#var hex_pos = flat_hex_to_world_position(AxialCoordinates.new(randi_range(-20, 20), randi_range(-20, 20)))
|
||||
#
|
||||
#tile_copy.position = Vector3(hex_pos.x, 20.0, hex_pos.y)
|
||||
#var target_pos = Vector3(hex_pos.x, 0.0, hex_pos.y)
|
||||
#
|
||||
#add_child(tile_copy)
|
||||
#
|
||||
#var tween = get_tree().create_tween()
|
||||
#tween.tween_property(tile_copy, "position", target_pos, 1.0).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_EXPO)
|
||||
#
|
||||
#func move_tile_to_random_position() -> void:
|
||||
#var new_hex_pos = flat_hex_to_world_position(AxialCoordinates.new(randi_range(-20, 20), randi_range(-20, 20)))
|
||||
#var sky_new_hex_pos = Vector3(new_hex_pos.x, 20.0, new_hex_pos.y)
|
||||
#var ground_new_hex_pos = Vector3(new_hex_pos.x, 0.0, new_hex_pos.y)
|
||||
#
|
||||
#var current_hex_pos = hex.position
|
||||
#var sky_current_hex_pos = hex.position + Vector3(0.0, 20.0, 0.0)
|
||||
#
|
||||
#var tween = get_tree().create_tween()
|
||||
#tween.tween_property(hex, "position", sky_current_hex_pos, 0.5).set_ease(Tween.EASE_IN).set_trans(Tween.TRANS_EXPO)
|
||||
#tween.tween_property(hex, "position", sky_new_hex_pos, 0.0)
|
||||
#tween.tween_property(hex, "position", ground_new_hex_pos, 1.0).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_EXPO)
|
||||
|
||||
func _process(delta) -> void:
|
||||
if Input.is_action_just_pressed("ui_accept"):
|
||||
print("yay")
|
||||
spawn_random_tile()
|
||||
|
||||
var pos3d = get_3d_pos(get_viewport().get_mouse_position())
|
||||
#if Input.is_action_just_pressed("ui_accept"):
|
||||
# print("yay")
|
||||
# spawn_random_tile()
|
||||
return
|
||||
if pos3d:
|
||||
var hex_pos = flat_hex_to_world_position(world_to_hex_tile(Vector2(pos3d.x, pos3d.z)))
|
||||
hex.position = Vector3(hex_pos.x, 0.0, hex_pos.y)
|
||||
coord_label.text = "%d, %d" % [hex_pos.x, hex_pos.y]
|
||||
|
||||
if current_tile == null:
|
||||
return
|
||||
|
||||
#var pos3d = get_3d_pos(get_viewport().get_mouse_position())
|
||||
#if pos3d:
|
||||
#var hex_pos = flat_hex_to_world_position(world_to_hex_tile(Vector2(pos3d.x, pos3d.z)))
|
||||
#current_tile.position = Vector3(hex_pos.x, 0.0, hex_pos.y)
|
||||
#coord_label.text = "%d, %d" % [hex_pos.x, hex_pos.y]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue