2022-08-04 10:50:28 -06:00
extends KinematicBody2D
class_name Player
export var MAX_SPEED : = 300
export var ACCELERATION : = 1000
export var FRICTION : = 2000
var player_velocity = Vector2 . ZERO
var state = State . IDLE
var follower : KinematicBody2D
2022-08-04 18:05:48 -06:00
var leader : = self
2022-08-04 10:50:28 -06:00
onready var sprite : = $ AnimatedSprite
2022-08-04 18:05:48 -06:00
onready var chevron_sprite : = $ ChevronSprite
2022-08-05 17:15:17 -06:00
onready var camera : = $ Camera2D
onready var gui : = $ GUI
2022-08-04 10:50:28 -06:00
enum State {
MOVE ,
2022-08-08 15:32:57 -06:00
IDLE ,
2022-08-04 10:50:28 -06:00
}
2022-08-05 17:15:17 -06:00
func set_camera_bounds ( up : int , down : int , left : int , right : int ) :
camera . set_limit ( MARGIN_TOP , up )
camera . set_limit ( MARGIN_BOTTOM , down )
camera . set_limit ( MARGIN_LEFT , left )
camera . set_limit ( MARGIN_RIGHT , right )
2022-08-04 10:50:28 -06:00
2022-08-05 17:15:17 -06:00
func _process ( delta : float ) - > void :
gui . global_position = camera . get_camera_screen_center ( ) - Vector2 ( get_viewport ( ) . get_visible_rect ( ) . size . x / 2 , get_viewport ( ) . get_visible_rect ( ) . size . y / 2 ) # Get Height)
2022-08-04 10:50:28 -06:00
func _physics_process ( delta : float ) - > void :
match state :
State . MOVE :
2022-08-04 18:05:48 -06:00
_move_state ( delta )
2022-08-04 10:50:28 -06:00
State . IDLE :
2022-08-04 18:05:48 -06:00
_idle_state ( delta )
chevron_sprite . visible = follower != null
if follower != null :
2022-08-05 17:15:17 -06:00
if follower . name == " Trolley " :
chevron_sprite . set_modulate ( Color ( 0 , 0.9 , 0 ) )
2022-08-04 18:05:48 -06:00
chevron_sprite . rotation_degrees = rad2deg ( ( follower . house_position - global_position ) . angle ( ) ) - 90
func _move_state ( delta : float ) - > void :
2022-08-04 10:50:28 -06:00
var input_vector : = Vector2 . ZERO
input_vector . x = Input . get_action_strength ( " ui_right " ) - Input . get_action_strength ( " ui_left " )
input_vector . y = Input . get_action_strength ( " ui_down " ) - Input . get_action_strength ( " ui_up " )
input_vector . normalized ( )
if input_vector . x < 0 and not sprite . flip_h :
sprite . flip_h = true
elif input_vector . x > 0 and sprite . flip_h :
sprite . flip_h = false
if input_vector != Vector2 . ZERO :
player_velocity = player_velocity . move_toward ( input_vector * MAX_SPEED , ACCELERATION * delta )
else :
player_velocity = player_velocity . move_toward ( Vector2 . ZERO , FRICTION * delta )
2022-08-08 15:32:57 -06:00
player_velocity = move_and_slide ( player_velocity , Vector2 ( 0 , 0 ) , false , 4 , 0.785398 , false )
for i in get_slide_count ( ) :
var collision : = get_slide_collision ( i )
if " Trolley " in collision . collider . name or " Car " in collision . collider . name :
state = State . IDLE
2022-08-04 10:50:28 -06:00
if player_velocity == Vector2 . ZERO :
state = State . IDLE
2022-08-04 18:05:48 -06:00
func _idle_state ( delta : float ) - > void :
2022-08-04 10:50:28 -06:00
if Input . is_action_just_pressed ( " ui_up " ) or Input . is_action_just_pressed ( " ui_down " ) or Input . is_action_just_pressed ( " ui_left " ) or Input . is_action_just_pressed ( " ui_right " ) :
sprite . set_animation ( " run " )
state = State . MOVE
else :
sprite . set_animation ( " idle " )
2022-08-08 15:32:57 -06:00
func _set_paused ( paused : bool ) - > void :
set_physics_process ( ! paused )
set_process ( ! paused )