Trying new stuff with the camera and grid. I want to be able to move a unit but there is a problem with the grid at the moment. I am now trying to implement a new way of handling the camera, maybe then I can move on to moving units...
This commit is contained in:
93
unit.gd
Normal file
93
unit.gd
Normal file
@@ -0,0 +1,93 @@
|
||||
## Represents a unit on the game board.
|
||||
## The board manages its position inside the game grid.
|
||||
## The unit itself holds stats and a visual representation that moves smoothly in the game world.
|
||||
@tool
|
||||
class_name Unit_Move
|
||||
extends Path2D
|
||||
|
||||
## Emitted when the unit reached the end of a path along which it was walking.
|
||||
signal walk_finished
|
||||
|
||||
## Shared resource of type Grid, used to calculate map coordinates.
|
||||
@export var grid: Resource
|
||||
## Distance to which the unit can walk in cells.
|
||||
@export var move_range := 6
|
||||
## The unit's move speed when it's moving along a path.
|
||||
@export var move_speed := 600.0
|
||||
## Texture representing the unit.
|
||||
@export var skin: Texture:
|
||||
set(value):
|
||||
skin = value
|
||||
if not _sprite:
|
||||
# This will resume execution after this node's _ready()
|
||||
await ready
|
||||
_sprite.texture = value
|
||||
## Offset to apply to the `skin` sprite in pixels.
|
||||
@export var skin_offset := Vector2.ZERO:
|
||||
set(value):
|
||||
skin_offset = value
|
||||
if not _sprite:
|
||||
await ready
|
||||
_sprite.position = value
|
||||
|
||||
## Coordinates of the current cell the cursor moved to.
|
||||
var cell := Vector2.ZERO:
|
||||
set(value):
|
||||
# When changing the cell's value, we don't want to allow coordinates outside
|
||||
# the grid, so we clamp them
|
||||
cell = grid.clamp(value)
|
||||
## Toggles the "selected" animation on the unit.
|
||||
var is_selected := false:
|
||||
set(value):
|
||||
is_selected = value
|
||||
if is_selected:
|
||||
_sprite.play("selected")
|
||||
else:
|
||||
_sprite.play("idle")
|
||||
|
||||
var _is_walking := false:
|
||||
set(value):
|
||||
_is_walking = value
|
||||
set_process(_is_walking)
|
||||
|
||||
@onready var _sprite: AnimatedSprite2D = $PathFollow2D/AnimatedSprite2D
|
||||
#@onready var _anim_player: AnimationPlayer = $AnimationPlayer
|
||||
@onready var _path_follow: PathFollow2D = $PathFollow2D
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
set_process(false)
|
||||
_path_follow.rotates = false
|
||||
|
||||
cell = grid.calculateGridCoordinates(position)
|
||||
position = grid.calculateMapPosition(cell)
|
||||
|
||||
# We create the curve resource here because creating it in the editor prevents us from
|
||||
# moving the unit.
|
||||
if not Engine.is_editor_hint():
|
||||
curve = Curve2D.new()
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
_path_follow.progress += move_speed * delta
|
||||
|
||||
if _path_follow.progress_ratio >= 1.0:
|
||||
_is_walking = false
|
||||
# Setting this value to 0.0 causes a Zero Length Interval error
|
||||
_path_follow.progress = 0.00001
|
||||
position = grid.calculateMapPosition(cell)
|
||||
curve.clear_points()
|
||||
emit_signal("walk_finished")
|
||||
|
||||
|
||||
## Starts walking along the `path`.
|
||||
## `path` is an array of grid coordinates that the function converts to map coordinates.
|
||||
func walk_along(path: PackedVector2Array) -> void:
|
||||
if path.is_empty():
|
||||
return
|
||||
|
||||
curve.add_point(Vector2.ZERO)
|
||||
for point in path:
|
||||
curve.add_point(grid.calculateMapPosition(point) - position)
|
||||
cell = path[-1]
|
||||
_is_walking = true
|
||||
Reference in New Issue
Block a user