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:
@@ -2,36 +2,60 @@ extends Node2D
|
||||
|
||||
class_name Unit
|
||||
|
||||
signal walk_finished
|
||||
|
||||
@export var grid: Resource
|
||||
|
||||
var _spawnPosition: Vector2
|
||||
|
||||
### Marker
|
||||
# For now the marker will be spawned and deleted by the unit.
|
||||
# Later it will be handled by the main scene.
|
||||
# Load marker scene.
|
||||
# var marker_scene: PackedScene = preload("res://Scenes/Unit/marker.tscn")
|
||||
# var marker
|
||||
## WE NOW USE A SIMPLE SPRITE2D FOR THE MARKER
|
||||
|
||||
|
||||
@onready var _readyToSelectMarker: Sprite2D = $ReadyToSelectMarker
|
||||
@onready var _selectedMarker: Sprite2D = $SelectedMarker
|
||||
#@onready var _movingMarker: Sprite2D = $MovingMarker
|
||||
|
||||
var _readyToSelect: bool = false
|
||||
var _selected: bool = false
|
||||
#var _moving: bool = false
|
||||
|
||||
var isSelected := false:
|
||||
set(value):
|
||||
isSelected = value
|
||||
if isSelected: _selectUnit()
|
||||
else: _deselectUnit()
|
||||
|
||||
var TargetPosition: Vector2
|
||||
var _isMoving := false:
|
||||
set(value):
|
||||
_isMoving = value
|
||||
set_process(_isMoving)
|
||||
|
||||
## Unit Data
|
||||
### Distance in cells
|
||||
@export var moveRange := 6
|
||||
### Speed along the path
|
||||
@export var moveSpeed := 600.0
|
||||
|
||||
# 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.
|
||||
cell = grid.clamp(value)
|
||||
@onready var _path: Path2D = $Path2D
|
||||
@onready var _pathFollow: PathFollow2D = $Path2D/PathFollow2D
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
# marker = marker_scene.instantiate()
|
||||
# marker.hide()
|
||||
# Spawning
|
||||
global_position = _spawnPosition
|
||||
|
||||
_readyToSelectMarker.hide()
|
||||
_selectedMarker.hide()
|
||||
# _movingMarker.hide()
|
||||
|
||||
# Pathing and Grid
|
||||
set_process(false)
|
||||
_pathFollow.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():
|
||||
_path.curve = Curve2D.new()
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
@@ -42,31 +66,13 @@ func _input(event: InputEvent):
|
||||
print_debug("Action is Select")
|
||||
|
||||
# We combine it with the fact that it is already marked (@see _markUnit)
|
||||
if _readyToSelect: _selectUnit()
|
||||
else: _deselectUnit()
|
||||
|
||||
|
||||
# if event.is_action_pressed("SetMarker"):
|
||||
# print("Action is SetMarker")
|
||||
# if _selected:
|
||||
# print("Setting marker to ", event.position)
|
||||
# #marker.global_position = event.position
|
||||
## marker.show()
|
||||
## _movingMarker.position = get_global_mouse_position()
|
||||
## _movingMarker.show()
|
||||
|
||||
func _getUnitPosition():
|
||||
return $AnimatedSprite2D.global_position
|
||||
|
||||
func _setSelected(selected: bool):
|
||||
_selected = selected
|
||||
if _readyToSelect: isSelected = true
|
||||
else: isSelected = false
|
||||
|
||||
func _selectUnit():
|
||||
_setSelected(true)
|
||||
_selectedMarker.show()
|
||||
|
||||
func _deselectUnit():
|
||||
_setSelected(false)
|
||||
_selectedMarker.hide()
|
||||
|
||||
func _gets_selected(viewport: Node, event: InputEvent, shape_index: int):
|
||||
@@ -82,4 +88,4 @@ func _unMarkUnit():
|
||||
_readyToSelectMarker.hide()
|
||||
|
||||
func moveToTarget():
|
||||
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user