You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.1 KiB
90 lines
2.1 KiB
extends Node
|
|
|
|
class_name Entity
|
|
|
|
var position: Vector3
|
|
var position2D: Vector2i
|
|
var movement: int = -1
|
|
var movement_step: float = 0.0
|
|
var id = -1
|
|
|
|
signal moving
|
|
|
|
func _init(id: int, position: Vector2i):
|
|
self.id = id
|
|
self.position.x = position.x
|
|
self.position.y = Global.world.get_height(Vector2i(position.x, position.y))
|
|
self.position.z = position.y
|
|
position2D = position
|
|
|
|
func get_data():
|
|
var data = {
|
|
"id": self.id,
|
|
"position": self.position
|
|
}
|
|
|
|
return data
|
|
|
|
func move(new_position: Vector2i):
|
|
Global.world.blocs[position.x][position.y].entity = -1
|
|
self.position.x = new_position.x
|
|
self.position.y = Global.world.get_height(Vector2i(new_position.x, new_position.y))
|
|
self.position.z = new_position.y
|
|
position2D = new_position
|
|
|
|
Global.world.blocs[new_position.x][new_position.y].entity = id
|
|
emit_signal("moving", self.position)
|
|
|
|
### Mettre un speed
|
|
func follow_path(path: Array):
|
|
for next in path:
|
|
var t = Timer.new()
|
|
t.wait_time = 0.3
|
|
t.set_one_shot(true)
|
|
t.autostart = true
|
|
Engine.get_main_loop().get_root().add_child(t)
|
|
await t.timeout
|
|
move(next)
|
|
t.queue_free()
|
|
|
|
|
|
func heuristic(a: Vector2i, b: Vector2i) -> float:
|
|
return abs(a.x - b.x) + abs(a.y - b.y)
|
|
|
|
func pathfinding(goal: Vector2i):
|
|
var frontier = []
|
|
var priorities = {}
|
|
var came_from = {}
|
|
var cost_so_far = {}
|
|
came_from[position2D] = null
|
|
cost_so_far[position2D] = 0
|
|
frontier.append(position2D)
|
|
|
|
while frontier.size():
|
|
var current = frontier.pop_front()
|
|
|
|
if current == goal:
|
|
break
|
|
|
|
for next in Global.world.neighbours(current):
|
|
var new_cost = cost_so_far[current] + Global.world.cost(current, next)
|
|
if next not in cost_so_far or new_cost < cost_so_far[next]:
|
|
cost_so_far[next] = new_cost
|
|
var priority = new_cost + heuristic(goal, next)
|
|
frontier.append(next)
|
|
priorities[next] = priority
|
|
came_from[next] = current
|
|
frontier.sort_custom(
|
|
func(a, b):
|
|
if priorities[a] < priorities[b]:
|
|
return true
|
|
return false
|
|
)
|
|
|
|
var current = goal
|
|
var path = []
|
|
while current != position2D:
|
|
path.append(current)
|
|
current = came_from[current]
|
|
path.reverse()
|
|
return path
|
|
|