-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.gd
236 lines (203 loc) · 7.53 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
extends Node2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var rng = RandomNumberGenerator.new()
# var bulletTexture = load("res://Assets/orb1.png")
var verticalSpeed = 0
var falling = true
var jumpReload = 0.5
var timeJumped = 0
var downAcceleration = 0.05
var fireRateBonus = 0.0
var damageBonus = 0.0
var speedBonus = 0.0
var pierceBonus = 0.0
var spreadBonus = 0.0
var fireBonus = 0
var iceBonus = 0
# var stepRotation = 1*2*3.14/180
# var shootRotationMultiplier = -15
var downGravity = 0.05
var maxLateralSpeed = 3
var currentLateralSpeed = 0
var lateralAcceleration = 0.1
var verticalSpeedConstant = 2
var maxVerticalSpeed = 4
var jumpSpeed = 3
var groundPosition = 563
var ceilingPosition = 120
var rightWallPosition = 1150
var leftWallPosition = 135
var score = 0
var maxScore = 0
# onready var orbs = [Orb.new("Crossbow", get_parent(), self), Orb.new("Water Gun", get_parent(), self), Orb.new("Fan", get_parent(), self)]
var orbs = []
var selectedOrb = 0
var changeOrbDelay = 0.3
var changeOrbTime = changeOrbDelay + 1
var nextLevel = 10
func _ready():
get_parent().get_node("Song").play()
maxScore = Variables.maxScore
get_parent().get_node("GUI/MaxScore").text = "Max Score: " + str(Variables.maxScore)
for orbName in Variables.initialOrbs:
orbs.append(Orb.new(orbName, get_parent().get_node("Bullets"), self))
orbs[0].equip()
orbs[selectedOrb].select()
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
changeOrbTime += delta
timeJumped += delta
# Orb change inputs
if Input.is_action_pressed("Change Orb Left"):
if changeOrbTime > changeOrbDelay:
var index = (selectedOrb - 1) % len(orbs)
changeOrb(index)
changeOrbTime = 0
if Input.is_action_pressed("Change Orb Right"):
if changeOrbTime > changeOrbDelay:
var index = (selectedOrb + 1) % len(orbs)
changeOrb(index)
changeOrbTime = 0
# Shooting inputs
if Input.is_action_pressed("click"):
orbs[selectedOrb].shoot()
else:
var shootDirection = Vector2(0, 0)
if Input.is_action_pressed("shoot_left"):
shootDirection.x -= 1
# orbs[selectedOrb].shootDirection(Vector2(-1, 0))
if Input.is_action_pressed("shoot_right"):
shootDirection.x += 1
# orbs[selectedOrb].shootDirection(Vector2(1, 0))
if Input.is_action_pressed("shoot_up"):
shootDirection.y -= 1
# orbs[selectedOrb].shootDirection(Vector2(0, -1))
if Input.is_action_pressed("shoot_down"):
shootDirection.y += 1
# orbs[selectedOrb].shootDirection(Vector2(0, 1))
if shootDirection.length_squared() > 0:
orbs[selectedOrb].shootDirection(shootDirection.normalized())
# Movement inputs
if Input.is_action_pressed("jump"):
jump()
if Input.is_action_pressed("descend"):
verticalSpeed = max(0, verticalSpeed)
verticalSpeed += downAcceleration
if Input.is_action_pressed("move_left"):
currentLateralSpeed = max(-maxLateralSpeed, currentLateralSpeed - lateralAcceleration)
$Body.flip_h = true
$Sprite.flip_h = true
if not falling:
$Body.play("Run")
elif Input.is_action_pressed("move_right"):
currentLateralSpeed = min(maxLateralSpeed, currentLateralSpeed + lateralAcceleration)
$Body.flip_h = false
$Sprite.flip_h = false
if not falling:
$Body.play("Run")
else:
if falling:
currentLateralSpeed *= 0.99
$Body.play("Fall")
else:
currentLateralSpeed *= 0.9
$Body.play("Idle")
# Update velocity and position
if falling:
verticalSpeed += downGravity
else:
verticalSpeed = 0
var moveVector = Vector2(currentLateralSpeed, verticalSpeed*verticalSpeedConstant)
translate(moveVector)
# Check for collisions
if falling:
if self.position.y > groundPosition:
self.position = Vector2(self.position.x, groundPosition)
verticalSpeed = 0
falling = false
$Body.play("Idle")
if self.position.y <= ceilingPosition:
self.position = Vector2(self.position.x, ceilingPosition)
verticalSpeed = 0
if self.position.x > rightWallPosition:
self.position = Vector2(rightWallPosition, self.position.y)
currentLateralSpeed = 0
elif self.position.x < leftWallPosition:
self.position = Vector2(leftWallPosition, self.position.y)
currentLateralSpeed = 0
func jump():
if timeJumped > jumpReload:
$Body.play("Jump")
if falling:
var index = rng.randi_range(1, 3)
if index == 1:
get_parent().get_node("Float1Sound").play()
elif index == 2:
get_parent().get_node("Float2Sound").play()
elif index == 3:
get_parent().get_node("Float3Sound").play()
else:
get_parent().get_node("JumpSound").play()
verticalSpeed -= jumpSpeed
verticalSpeed = max(-maxVerticalSpeed, verticalSpeed)
# print("vertical speed: " + str(verticalSpeed))
falling = true
timeJumped = 0
# func shoot(direction):
# # create a new bullet
# # var bullet = Bullet.new(position, direction, 2, 3, 1, "linear", 0)
# # get_parent().add_child(bullet)
# #BulletAttack(parent: Node, position: Vector2, direction: Vector2, bulletMovement: String, bulletsPerShot: int, spreadAngle: float, spreadDistribution: String, damagePerBullet: int, bulletSpeed: float, pierce: int, bulletLifetime: float):
# BulletAttack.shoot(get_parent(), position, direction, "Linear", 5, 45, "Equidistant", 1, 10, 0, 0.5)
func addScore(points):
self.score += points
get_parent().get_node("GUI/Score").text = str(self.score)
if score > maxScore:
maxScore = score
get_parent().get_node("GUI/MaxScore").text = "Max Score: "+str(maxScore)
Variables.maxScore = maxScore
var file = File.new()
file.open("user://MaxScore.save", File.WRITE)
file.store_var(maxScore)
file.close()
if score >= nextLevel:
nextLevel += nextLevel + pow((nextLevel/4),2)
get_tree().paused = true
get_parent().get_node("OwnedOrbSelection").visible = true
get_parent().get_node("OwnedOrbSelection").initialize(orbs)
func _on_RigidBody2D_body_entered(body:Node):
print(body)
# pass # Replace with function body.
func _on_RigidBody2D_mouse_entered():
print("endeter mouse")
pass # Replace with function body.
func _on_Area2D_body_entered(body:Node):
print(body)
# pass # Replace with function body.
func _on_Area2D_area_entered(area:Area2D):
# print(area.get_parent().damage)
# print(area.get_parent())
pass # Replace with function body.
func _on_Body_animation_finished():
# if animation is jump
if $Body.animation == "Jump":
if falling:
$Body.animation = "Fall"
else:
$Body.animation = "Idle"
pass # Replace with function body.
func changeOrb(index: int):
$"../OrbSwitch".play()
yield(get_tree().create_timer(0.1, false), "timeout")
orbs[selectedOrb].deselect()
selectedOrb = index
orbs[selectedOrb].select()
func getUpgrade():
get_tree().paused = true
func _on_OwnedOrbSelection_orbSelected(index):
orbs[index].upgrade()
get_tree().paused = false
get_parent().get_node("OwnedOrbSelection").visible = false