Initial commit
This commit is contained in:
39
nodes/combat_map.gd
Normal file
39
nodes/combat_map.gd
Normal file
@@ -0,0 +1,39 @@
|
||||
class_name CombatMap
|
||||
extends Node2D
|
||||
|
||||
signal tile_hovered(coords: Vector2i)
|
||||
|
||||
@export var tile_set: DLTileset
|
||||
@onready var tile_map: TileMapLayer = %TerrainLayer
|
||||
@onready var highlight_map: GridOverlay = %OverlayLayer
|
||||
|
||||
const TILE_SIZE := 48.0
|
||||
const SOURCE_ID: int = 0
|
||||
|
||||
|
||||
func snap_to_grid(pos: Vector2) -> Vector2:
|
||||
return Vector2(floorf(pos.x / TILE_SIZE), floorf(pos.y / TILE_SIZE)) * TILE_SIZE
|
||||
|
||||
|
||||
func world_to_coords(pos: Vector2) -> Vector2i:
|
||||
return Vector2i(snap_to_grid(pos) / TILE_SIZE)
|
||||
|
||||
|
||||
func coords_to_world(coords: Vector2i) -> Vector2:
|
||||
return Vector2(coords) * TILE_SIZE
|
||||
|
||||
func draw_wall(coords: Vector2i) -> void:
|
||||
draw_custom(coords, tile_set.wall_tile_coords)
|
||||
|
||||
func draw_floor(coords: Vector2i) -> void:
|
||||
draw_custom(coords, tile_set.floor_tile_coords)
|
||||
|
||||
func draw_custom(coords: Vector2i, tile_coords: Vector2i) -> void:
|
||||
tile_map.set_cell(coords, SOURCE_ID, tile_coords)
|
||||
|
||||
func is_wall(coords: Vector2i) -> bool:
|
||||
return tile_map.get_cell_atlas_coords(coords) == tile_set.wall_tile_coords
|
||||
|
||||
func target_tile(coords: Vector2i) -> void:
|
||||
highlight_map.target_tile(coords)
|
||||
tile_hovered.emit(coords)
|
||||
1
nodes/combat_map.gd.uid
Normal file
1
nodes/combat_map.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bks7uplgjjdg0
|
||||
80
nodes/player_controller.gd
Normal file
80
nodes/player_controller.gd
Normal file
@@ -0,0 +1,80 @@
|
||||
class_name PlayerController extends Node
|
||||
|
||||
const SPEED = 192.0
|
||||
|
||||
@export var dl_map: CombatMap
|
||||
|
||||
var _selected_unit: Unit = null
|
||||
var _target_pos: Vector2
|
||||
var _goal_pos: Vector2
|
||||
var _moving := false
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseButton and not event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
||||
var world_pos: Vector2 = get_viewport().get_canvas_transform().affine_inverse() * event.position
|
||||
var clicked_unit := _get_unit_at(world_pos)
|
||||
|
||||
if clicked_unit:
|
||||
_select_unit(clicked_unit)
|
||||
get_viewport().set_input_as_handled()
|
||||
elif _selected_unit:
|
||||
var snapped_pos := dl_map.snap_to_grid(world_pos)
|
||||
var grid_coords := dl_map.world_to_coords(world_pos)
|
||||
if dl_map.is_wall(grid_coords):
|
||||
return
|
||||
_goal_pos = snapped_pos
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if not _selected_unit:
|
||||
return
|
||||
|
||||
if _moving:
|
||||
var remaining := _target_pos - _selected_unit.position
|
||||
var step := SPEED * delta
|
||||
|
||||
if remaining.length() <= step:
|
||||
_selected_unit.position = _target_pos
|
||||
_moving = false
|
||||
else:
|
||||
_selected_unit.position += remaining.normalized() * step
|
||||
return
|
||||
|
||||
if _selected_unit.position != _goal_pos:
|
||||
var diff := _goal_pos - _selected_unit.position
|
||||
var dir: Vector2
|
||||
if absf(diff.x) >= absf(diff.y):
|
||||
dir = Vector2(signf(diff.x), 0)
|
||||
else:
|
||||
dir = Vector2(0, signf(diff.y))
|
||||
|
||||
var next_pos := _selected_unit.position + dir * dl_map.TILE_SIZE
|
||||
var grid_coords := dl_map.world_to_coords(next_pos)
|
||||
if dl_map.is_wall(grid_coords):
|
||||
_goal_pos = _selected_unit.position
|
||||
return
|
||||
|
||||
_target_pos = next_pos
|
||||
_moving = true
|
||||
|
||||
|
||||
func _select_unit(unit: Unit) -> void:
|
||||
if _selected_unit:
|
||||
_selected_unit.set_selected(false)
|
||||
_selected_unit = unit
|
||||
_selected_unit.set_selected(true)
|
||||
_goal_pos = _selected_unit.position
|
||||
_target_pos = _selected_unit.position
|
||||
_moving = false
|
||||
|
||||
|
||||
func _get_unit_at(world_pos: Vector2) -> Unit:
|
||||
var snapped := dl_map.snap_to_grid(world_pos)
|
||||
for child in get_parent().get_children():
|
||||
if child is Unit:
|
||||
var unit_snapped := dl_map.snap_to_grid(child.global_position)
|
||||
if unit_snapped == snapped:
|
||||
return child
|
||||
return null
|
||||
1
nodes/player_controller.gd.uid
Normal file
1
nodes/player_controller.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dfojm3n0em4ef
|
||||
23
nodes/unit.gd
Normal file
23
nodes/unit.gd
Normal file
@@ -0,0 +1,23 @@
|
||||
class_name Unit extends Node2D
|
||||
|
||||
#region Templates
|
||||
@export var stat_template: UnitStats
|
||||
@export var info_template: UnitInfo
|
||||
@export var allegiance_template: UnitAllegiance
|
||||
#endregion
|
||||
|
||||
var current_stats: UnitStats
|
||||
var current_info: UnitInfo
|
||||
var current_allegiance: UnitAllegiance
|
||||
|
||||
signal unit_selected_changed(unit: Unit, selected: bool)
|
||||
signal unit_allegiance_changed(unit: Unit, allegiance: UnitAllegiance)
|
||||
|
||||
func _ready() -> void:
|
||||
current_stats = stat_template.duplicate(true)
|
||||
current_info = info_template.duplicate(true)
|
||||
current_allegiance = allegiance_template.duplicate(true)
|
||||
unit_allegiance_changed.emit(self, current_allegiance)
|
||||
|
||||
func set_selected(selected: bool) -> void:
|
||||
unit_selected_changed.emit(self, selected)
|
||||
1
nodes/unit.gd.uid
Normal file
1
nodes/unit.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://c016mxgatcpse
|
||||
Reference in New Issue
Block a user