-
Notifications
You must be signed in to change notification settings - Fork 4
/
Player.gd
102 lines (85 loc) · 2.56 KB
/
Player.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
extends KinematicBody2D
class_name Player
signal hud
export var gravity : float = 60
var horizontal : int = 0
var vertical : int = 0
var up : bool = false
var velocity: Vector2 = Vector2.ZERO
var vx: float = 0 setget _set_vx, _get_vx
var vy: float = 0 setget _set_vy, _get_vy
var underwater : bool = false
var grounded : bool = false setget ,_get_grounded
var jumping : bool = false setget ,_get_jumping
var ladder_area : bool = false
var ladder_tip : bool = false
var ladder_x : float
onready var jump_timer : Timer = $Timers/JumpTimer
onready var floor_timer : Timer = $Timers/FloorTimer
onready var ladder_timer : Timer = $Timers/LadderTimer
onready var platform_timer : Timer = $Timers/PlatformTimer
onready var sprite : Sprite = $Sprite
onready var anim : AnimationPlayer = $Sprite/AnimationPlayer
onready var state_machine: PlayerFSM = $PlayerStates
onready var tween : Tween = $Tween
onready var waves : Particles2D = $Waves
func _ready():
state_machine.init(self)
func _physics_process(delta):
update_inputs()
state_machine.run()
emit_signal("hud", "%s" % state_machine.active_state.tag)
func update_inputs():
horizontal = (
int(Input.is_action_pressed("ui_right"))
- int(Input.is_action_pressed("ui_left"))
)
vertical = (
int(Input.is_action_pressed("ui_down"))
- int(Input.is_action_pressed("ui_up"))
)
up = Input.is_action_pressed("ui_up")
if Input.is_action_just_pressed("ui_accept"):
jump_timer.start()
if is_on_floor():
floor_timer.start()
func move():
var old = velocity
velocity = move_and_slide(velocity, Vector2.UP, true)
func apply_gravity (gravity:float):
velocity += Vector2.DOWN * gravity
func play(animation:String):
if anim.current_animation == animation:
return
anim.play(animation)
func tween_to_ladder():
var target = Vector2(ladder_x, position.y)
tween.interpolate_property(self, "position", position, target,
0.05, Tween.TRANS_LINEAR, Tween.EASE_OUT)
tween.start()
func can_climb():
return ladder_area and ladder_timer.is_stopped()
###########################################################
# Setget
###########################################################
func _get_vx():
return vx
func _set_vx(val:float):
if val != 0:
sprite.flip_h = (val < 0)
velocity.x = val
vx = val
func _get_vy():
return vy
func _set_vy(val:float):
velocity.y = val
vy = val
func _get_grounded():
grounded = not floor_timer.is_stopped()
return grounded
func _get_jumping():
jumping = not jump_timer.is_stopped()
return jumping
###########################################################
func _on_PlatformTimer_timeout():
collision_layer = 1 | 2