-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCharacter.gd
64 lines (45 loc) · 1.59 KB
/
Character.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
extends KinematicBody2D
class_name Character
const FRICTION: float = 0.15
const GRAVITY: int = 300
var can_move: bool = true setget set_can_move
export(int) var speed: int = 120
var velocity: Vector2 = Vector2.ZERO
var is_immune: bool = false
export(int) var max_hp: int = 2 setget set_max_hp
var hp: int = max_hp setget set_hp
signal hp_changed(previous_hp, actual_hp)
signal killed()
signal max_hp_increased()
export(int) var damage: int = 1
onready var state_machine: Node = get_node("FiniteStateMachine")
onready var sprite: Sprite = get_node("Sprite")
onready var collision_shape: CollisionShape2D = get_node("CollisionShape2D")
onready var hitbox: Area2D = get_node_or_null("HitBox")
func apply_gravity(delta: float) -> void:
velocity.y += GRAVITY * delta
func apply_friction() -> void:
velocity.x = lerp(velocity.x, 0.0, FRICTION)
func _physics_process(_delta: float) -> void:
velocity = move_and_slide(velocity, Vector2.UP)
func take_damage(dam: int, direction: int) -> void:
if not is_immune:
self.hp -= dam
if hp > 0:
velocity += Vector2(direction * 150, -50)
state_machine.set_state(state_machine.states.hurt)
else:
velocity += Vector2(direction * 250, -50)
state_machine.set_state(state_machine.states.dead)
func set_hp(new_hp: int) -> void:
var previous_hp: int = hp
hp = int(clamp(new_hp, 0, max_hp))
emit_signal("hp_changed", previous_hp, hp)
if hp == 0:
emit_signal("killed")
func set_max_hp(new_max: int) -> void:
max_hp = new_max
emit_signal("max_hp_increased")
self.hp = max_hp
func set_can_move(new_value: bool) -> void:
can_move = new_value