Using the new MapGlobal at various parts and switched away from the Grid.resource
This commit is contained in:
@@ -1,16 +1,24 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
@export var SPEED = 400.0
|
||||
@export var unitData = {
|
||||
"MoveRange": 5,
|
||||
"MoveRangeV": Vector2i(5, 5),
|
||||
"Speed": 400.0
|
||||
}
|
||||
|
||||
var grid: AStarGrid2D
|
||||
var currentCell: Vector2i
|
||||
var currentPoint: int
|
||||
|
||||
var selected: bool:
|
||||
set(v):
|
||||
selected = v; $PathPrev.visible = selected;
|
||||
selected = v; $PathPrev.visible = selected; if selected: MapGlobal.SetRegion(calculateMovementRegion())
|
||||
var moving: bool:
|
||||
set(v):
|
||||
moving = v; $PathPrev.visible = not moving; set_physics_process(moving)
|
||||
|
||||
signal MoveFinished
|
||||
|
||||
var targetCell: Vector2i
|
||||
var movePts: Array
|
||||
|
||||
@@ -25,7 +33,7 @@ func setup(_grid: AStarGrid2D):
|
||||
targetCell = currentCell
|
||||
|
||||
func pos_to_cell(pos: Vector2):
|
||||
return pos / grid.cell_size
|
||||
return pos / Vector2(MapGlobal.getData()["cellSize"])
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if moving: return
|
||||
@@ -36,10 +44,12 @@ func setTarget(target: Vector2i):
|
||||
if !selected: return
|
||||
if moving: return
|
||||
if target != targetCell:
|
||||
movePts = grid.get_point_path(currentCell, target)
|
||||
print("Setting target: ")
|
||||
movePts = MapGlobal.GetGrid().get_point_path(currentCell, target)
|
||||
movePts = (movePts as Array).map(func (point): return point + grid.cell_size/2.0)
|
||||
$PathPrev.points = movePts
|
||||
targetCell = target
|
||||
print (targetCell)
|
||||
|
||||
|
||||
func startMove():
|
||||
@@ -51,10 +61,13 @@ func _physics_process(delta: float) -> void:
|
||||
velocity = Vector2.ZERO
|
||||
global_position = movePts[-1]
|
||||
currentCell = pos_to_cell(global_position)
|
||||
$PathPrev.points = []; moving = false
|
||||
$PathPrev.points = [];
|
||||
moving = false; MoveFinished.emit();
|
||||
MapGlobal.SetRegion(calculateMovementRegion(), unitData["MoveRangeV"])
|
||||
|
||||
else:
|
||||
var direction = (movePts[currentPoint+1] - movePts[currentPoint]).normalized()
|
||||
velocity = direction * SPEED
|
||||
velocity = direction * unitData["Speed"]
|
||||
move_and_slide()
|
||||
if (movePts[currentPoint+1] - global_position).length() < 4:
|
||||
currentCell = pos_to_cell(global_position)
|
||||
@@ -65,3 +78,8 @@ func selectUnit(cell: Vector2i):
|
||||
selected = true
|
||||
else:
|
||||
selected = false
|
||||
|
||||
func calculateMovementRegion():
|
||||
var region := Rect2i(pos_to_cell(global_position), Vector2i(unitData["MoveRange"], unitData["MoveRange"]))
|
||||
region.set_center
|
||||
return region
|
||||
|
||||
Reference in New Issue
Block a user