-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.gd
155 lines (134 loc) · 4.39 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
extends Area2D
@export var speed = 400 #(pixels/sec)
var screen_size
var isJumping = false
var isGravity = false
var jumped = 0
var previousVelocity
var animationHandle
@export var energy = 1000;
var characters = ["snowTiger", "panda"]
var panda_falling_frames = [];
var snowTiger_falling_frames = [];
var chosenCharacter = "snowTiger"
signal collect
signal hit
signal eat
signal characterSelect
signal jump
signal doubleJump
signal land
func _on_character_select(characterSelected):
print("character selected in Player " + characterSelected)
chosenCharacter = characterSelected
self.call("_ready")
# Called when the node enters the scene tree for the first time.
func _ready():
#self.connect("characterSelect", _on_character_select)
screen_size = get_viewport_rect().size
loadCharacterFramesBasedOnChosenCharacter(chosenCharacter)
func loadCharacterFramesBasedOnChosenCharacter(character_name):
print(character_name + "has been chosen")
var dynamicallyCreatedAnimatedSprite = createCharacterFrames(character_name)
self.add_child(dynamicallyCreatedAnimatedSprite)
animationHandle = dynamicallyCreatedAnimatedSprite
func createCharacterFrames(character_name):
var fallFrames = [character_name+"_jump_three.png"]
var jumpFrames = [character_name+"_stand.png",character_name+"_jump_one.png",character_name+"_jump_two.png",character_name+"_jump_three.png"]
var runFrames = [character_name+"_stand.png",character_name+"_jump_one.png"]
var animatedSprite = AnimatedSprite2D.new()
var frames = SpriteFrames.new()
frames.add_animation("Fall")
for frame_name in fallFrames:
var texture = load("res://images/"+frame_name) as Texture
frames.add_frame("Fall", texture)
frames.add_animation("Jump")
for frame_name in jumpFrames:
var texture = load("res://images/"+frame_name) as Texture
frames.add_frame("Jump", texture)
frames.add_animation("Run")
for frame_name in runFrames:
var texture = load("res://images/"+frame_name) as Texture
frames.add_frame("Run", texture)
animatedSprite.sprite_frames = frames
animatedSprite.scale.x = 0.5
animatedSprite.scale.y = 0.5
animatedSprite.connect("animation_finished", on_player_sprite_animation_finished)
animatedSprite.connect("animation_looped", on_player_sprite_animation_looped)
return animatedSprite
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var velocity
gravity = 50000
if previousVelocity:
velocity = previousVelocity
else:
velocity = Vector2.ZERO
#animationHandle.play()
animationHandle.play()
if Input.is_action_just_pressed("jump") && jumped < 2 && isJumping:
isJumping = true
isGravity = true
velocity = Vector2.ZERO
jumped = jumped + 1
emit_signal("doubleJump")
animationHandle.animation = "Run"
animationHandle.animation = "Jump"
if (Input.is_action_just_pressed("jump") && jumped < 2 || isJumping):
velocity = Vector2.ZERO
velocity.y -= 400
energy = energy - 2.5
#velocity = velocity.normalized() * speed
previousVelocity = velocity
isJumping = true
if(Input.is_action_just_pressed("jump")):
jumped = jumped + 1
emit_signal("jump")
animationHandle.animation = "Jump"
elif isGravity:
velocity = Vector2.ZERO
velocity.y += 1 + gravity * 1.5 * delta
#velocity = velocity.normalized() * speed
if position.y >= 400:
isGravity = false
emit_signal("land")
velocity = Vector2.ZERO
previousVelocity = velocity
else:
velocity = Vector2.ZERO
previousVelocity = velocity
isJumping = false
jumped = 0
animationHandle.animation = "Run"
if position.y >= 600:
isGravity = false
velocity = Vector2.ZERO
if animationHandle.animation == "Run":
energy = energy + 1.1
if energy > 1000:
energy = 1000
#fixes device lag causing player character getting caught under screen
if position.y > 400:
position.y = 400
position += velocity*delta
position = position.clamp(Vector2.ZERO, screen_size)
func on_player_sprite_animation_finished():
print("animation finished")
func on_player_sprite_animation_looped():
if animationHandle.animation == "Jump":
isGravity = true
isJumping = false
animationHandle.animation = "Fall"
func _on_body_entered(body):
if body.is_in_group("Coin"):
print("coin collected")
#collect.emit()
emit_signal("collect")
elif body.is_in_group("Spike"):
print("spike hit")
hide()
#hit.emit()
emit_signal("hit")
elif body.is_in_group("Food"):
print("food collected");
emit_signal("eat")