-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.gd
67 lines (56 loc) · 1.59 KB
/
Main.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
extends Node
export (PackedScene) var Coin
const LEVEL_MAX_TIME = 30
var score
var time
func _ready():
randomize()
newGame()
print(GameInput.getKey("version"))
func gameOver():
$LevelTimer.stop()
$CoinTimer.stop()
$HUD.showGameOver()
$Music.stop()
$DeathSound.play()
$HUD.updateTime(LEVEL_MAX_TIME)
func newGame():
score = 0
time = LEVEL_MAX_TIME
$HUD.updateTime(LEVEL_MAX_TIME)
$Player.start($StartPosition.position)
$StartTimer.start()
$HUD.updateScore(score)
$HUD.showMessage("Get Ready")
$HUD.hideStartButton()
$Music.play()
func _on_CoinTimer_timeout():
# Choose a random location on Path2D.
$CoinPath/CoinSpawnLocation.set_offset(randi())
# Create a Coin instance and add it to the scene.
var coin = Coin.instance()
add_child(coin)
# Set the coin's direction perpendicular to the path direction.
var direction = $CoinPath/CoinSpawnLocation.rotation + PI / 2
# Set the coin's position to a random location.
coin.position = $CoinPath/CoinSpawnLocation.position
# Add some randomness to the direction.
direction += rand_range(-PI / 4, PI / 4)
coin.rotation = direction
# Set the velocity (speed & direction).
coin.linear_velocity = Vector2(rand_range(coin.min_speed, coin.max_speed), 0)
coin.linear_velocity = coin.linear_velocity.rotated(direction)
# Connect delete of coins at game end
$HUD.connect("start_game", coin, "_on_start_game")
func _on_LevelTimer_timeout():
time -= 1
$HUD.updateTime(time)
if time <= 0:
gameOver()
func _on_StartTimer_timeout():
$CoinTimer.start()
$LevelTimer.start()
func _on_Player_hit():
if time > 0:
score += 1
$HUD.updateScore(score)