-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.gd
148 lines (133 loc) · 4.67 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
extends KinematicBody
signal souls_changed()
signal hp_changed()
signal im_dead(deadObject)
signal turned_side()
signal player_dead()
signal start_endphase()
var speed = 600
const FIRE = 1
const WATER = 0
const elemental_enum = ["water", "fire"]
const FIRE_GROUP = "FireElemental"
const WATER_GROUP = "WaterElemental"
var current_element = "water"
const MAX_HP = 500
var hp = MAX_HP
var power = 30 #TODO change later
var shield_is_active = false
var elemental_souls_counter = [10, 10]
const switch_costs_souls = 5 #change for editing the soul costs when switching
const SHIELD_POWER_UP_COSTS = 10
const SHIELD_DURATION = 10
const TELEPORT_POWER_UP_COSTS = 5
const TELEPORT_RANGE = 0.5 #should be between 0-1
var timeSinceLastHealthRecover
func _ready():
# Called every time the node is added to the scene.
# Initialization here
set_process(true)
update_elemental_color()
emit_signal("souls_changed")
emit_signal("hp_changed")
add_to_group(WATER_GROUP)
timeSinceLastHealthRecover = OS.get_unix_time()
$Shield.hide()
func _process(delta):
# # Called every frame. Delta is time since last frame.
# # Update game logic here.
var input = Vector3(0,0,0)
input.x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
input.z = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
move_and_slide(input.normalized() * speed * delta, Vector3(0,1,0))
if Input.is_action_just_pressed("ui_power_up_shield"):
useShieldPowerUp()
if Input.is_action_just_pressed("ui_power_up_teleport"):
useTeleportPowerUp(delta)
restoreHp()
if Input.is_action_just_pressed("ui_start_endphase"):
emit_signal("start_endphase")
#handles the elemental-group-changig !INCOMPELE!
if Input.is_action_just_pressed("ui_accept"):
tryChangingSide()
func tryChangingSide():
#check if the player has enough souls
if current_element == elemental_enum[WATER] and elemental_souls_counter[FIRE] >= switch_costs_souls:
current_element = elemental_enum[FIRE]
elemental_souls_counter[FIRE] -= switch_costs_souls
remove_from_group(WATER_GROUP)
add_to_group(FIRE_GROUP)
update_elemental_color()
yield(get_tree().create_timer(3), "timeout")
emit_signal("turned_side")
elif elemental_souls_counter[WATER] >= switch_costs_souls:
current_element = elemental_enum[WATER]
elemental_souls_counter[WATER] -= switch_costs_souls
remove_from_group(FIRE_GROUP)
add_to_group(WATER_GROUP)
update_elemental_color()
yield(get_tree().create_timer(3), "timeout")
emit_signal("turned_side")
#else:
#TODO add message to the player that indicates that he has not enough souls to switch
func update_elemental_color():
$Mesh.get_surface_material(0).albedo_color = Color(1, 0, 0) if current_element == elemental_enum[FIRE] else Color(0, 0, 1)
emit_signal("souls_changed") #why do the souls change?
emit_turn_particles()
func collect_soul(type):
#just get souls from the other type???????
if type == 'fire': #&& current_element == elemental_enum[WATER]:
elemental_souls_counter[FIRE] += 1
emit_signal("souls_changed")
else: #current_element == elemental_enum[FIRE]:
elemental_souls_counter[WATER] += 1
emit_signal("souls_changed")
power += 1
func takeDamage(damage):
if not shield_is_active:
hp -= damage
emit_signal("hp_changed")
if hp <= 0:
emit_signal("im_dead", self)
emit_signal("player_dead")
func emit_turn_particles():
var particles = preload("res://turn_particles.tscn").instance()
add_child(particles)
yield(get_tree().create_timer(1), "timeout")
particles.queue_free()
func useShieldPowerUp():
if not shield_is_active && power >= SHIELD_POWER_UP_COSTS:
power -= SHIELD_POWER_UP_COSTS
emit_signal("souls_changed")
shield_is_active = true
$Shield.show()
yield(get_tree().create_timer(SHIELD_DURATION), "timeout")
shield_is_active = false
$Shield.hide()
func useTeleportPowerUp(delta):
if power >= TELEPORT_POWER_UP_COSTS:
var direction = Vector3(0, 0, 0)
if Input.is_action_pressed("ui_right"):
direction.x += 1
if Input.is_action_pressed("ui_left"):
direction.x -= 1
if Input.is_action_pressed("ui_up"):
direction.z -= 1
if Input.is_action_pressed("ui_down"):
direction.z += 1
if direction.z != 0 or direction.x != 0:
power -= TELEPORT_POWER_UP_COSTS
emit_signal("souls_changed")
var current_pos = translation
look_at(direction.normalized() + current_pos, Vector3(0, 0, 0))
translate(direction.normalized() * speed * delta * TELEPORT_RANGE)
func restoreHp():
var currentTime = OS.get_unix_time()
var elapsedTime = currentTime - timeSinceLastHealthRecover
if elapsedTime >=1:
timeSinceLastHealthRecover = currentTime
if hp < MAX_HP:
hp += elapsedTime
if hp > MAX_HP:
hp = MAX_HP
emit_signal("hp_changed")