1
0
Fork 0
Cleanomancer/Script/Player.gd

133 lines
3.4 KiB
GDScript3
Raw Normal View History

2021-04-24 21:35:17 +02:00
tool
extends Area2D
2021-04-25 19:44:16 +02:00
class_name Player
2021-04-24 21:35:17 +02:00
2021-04-24 22:28:55 +02:00
export var speed : float = 1
2021-04-25 16:16:48 +02:00
export var speed_dash : float = 3
2021-04-24 22:28:55 +02:00
export var rest : float = 1
2021-04-25 23:32:24 +02:00
export var reservoir : int = 1
2021-04-24 21:35:17 +02:00
2021-04-25 19:44:16 +02:00
var level
2021-04-24 21:35:17 +02:00
2021-04-25 19:44:16 +02:00
var tile_position := Vector2(0,0)
2021-04-24 22:28:55 +02:00
var last_move_time := 0.0
2021-04-25 02:22:07 +02:00
var step_count := 0
var stepped := false
2021-04-25 16:16:48 +02:00
var dashing := false
2021-04-24 22:28:55 +02:00
2021-04-24 21:35:17 +02:00
class Movement:
var action: String
var direction: Vector2
var animation: String
func _init(_action, _direction, _animation):
action = _action
direction = _direction
animation = _animation
var movements = [
Movement.new("ui_right", Vector2.RIGHT, "right"),
Movement.new("ui_left", Vector2.LEFT, "left"),
Movement.new("ui_up", Vector2.UP, "back"),
Movement.new("ui_down", Vector2.DOWN, "front")
]
2021-04-25 23:32:24 +02:00
class Sweep:
var action: String
var tile_limit
var waste_limit
var cost: int
func _init(_action, _tlimit, _wlimit, _cost):
action = _action
tile_limit = _tlimit
waste_limit = _wlimit
cost = _cost
var sweeps = [
Sweep.new("dash", INF, INF, 3),
2021-04-25 23:52:18 +02:00
Sweep.new("whoosh", INF, 1, 2)
2021-04-25 23:32:24 +02:00
]
2021-04-24 21:35:17 +02:00
var movement_queue = []
var current_target = null
2021-04-25 16:16:48 +02:00
var current_direction : Movement = movements.back()
2021-04-24 21:35:17 +02:00
onready var sprite = $AnimatedSprite as AnimatedSprite
func _process(delta):
2021-04-25 19:44:16 +02:00
2021-04-24 21:35:17 +02:00
if Engine.editor_hint:
2021-04-25 19:44:16 +02:00
return
2021-04-26 01:44:38 +02:00
if Input.is_action_pressed("reset"):
get_tree().reload_current_scene()
2021-04-25 19:44:16 +02:00
if (last_move_time + rest) * 1000 < OS.get_ticks_msec():
for movement in movements:
if Input.is_action_pressed(movement.action):
current_direction = movement
if not Input.is_action_pressed("lock_move"):
var target = level.get_tile_v(tile_position + movement.direction)
if target.can_walk() or target.can_push(movement.direction):
movement_queue.push_back(movement)
update_animation()
if not movement_queue.empty():
var current_movement : Movement = movement_queue.front()
if current_target == null:
current_target = tile_position + current_movement.direction
var tile = level.get_tile_v(current_target)
if not tile.can_walk() and not tile.can_push(current_movement.direction):
current_target = null
movement_queue.pop_front()
else:
tile.push(current_movement.direction)
2021-04-24 21:35:17 +02:00
2021-04-25 19:44:16 +02:00
if current_target != null:
tile_position += current_movement.direction * delta * (speed_dash if dashing else speed)
2021-04-24 21:35:17 +02:00
2021-04-25 19:44:16 +02:00
var remaining : Vector2 = current_target - tile_position
2021-04-24 21:35:17 +02:00
2021-04-25 19:44:16 +02:00
if remaining.dot(current_movement.direction) <= 0:
tile_position = current_target
current_target = null
movement_queue.pop_front()
stepped = false
dashing = false
elif remaining.length() < 0.5 and not stepped:
step_count += 1
stepped = true
2021-04-24 21:35:17 +02:00
update_position()
2021-04-25 19:44:16 +02:00
last_move_time = OS.get_ticks_msec() / 1000.0
2021-04-24 21:35:17 +02:00
2021-04-25 23:32:24 +02:00
for sweep in sweeps:
2021-04-24 21:35:17 +02:00
2021-04-25 23:32:24 +02:00
if reservoir >= sweep.cost and Input.is_action_just_pressed(sweep.action):
2021-04-24 21:35:17 +02:00
2021-04-25 23:32:24 +02:00
if not current_target == null:
tile_position = current_target
var current = level.get_tile_v(tile_position)
var end = current.dash(current_direction.direction, sweep.tile_limit, sweep.waste_limit)
if current != end:
movement_queue = [current_direction]
current_target = Vector2 (end.x, end.y)
dashing = true
reservoir = reservoir - sweep.cost
2021-04-26 01:24:29 +02:00
level.update_reservoir()
2021-04-25 23:32:24 +02:00
break
2021-04-24 21:35:17 +02:00
func update_position():
position = level.cell_custom_transform * (tile_position + Vector2.ONE / 2 )
2021-04-24 21:35:17 +02:00
func update_animation():
2021-04-25 16:16:48 +02:00
sprite.animation = current_direction.animation
2021-04-26 01:24:29 +02:00
sprite.frame = step_count % 2 + (0 if reservoir > 0 else 2)