summaryrefslogtreecommitdiff
path: root/player.gd
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-09-10 18:16:15 +0200
committerDaniel Weipert <git@mail.dweipert.de>2024-09-10 18:16:15 +0200
commit3fdb011ef62249a514a8eba2556cc9f6ea5ea477 (patch)
tree3273fe2e4736f6843d1a86ae0bcf5901ffbedc86 /player.gd
parentd200edc1a73f4cb8e7595579a8914301b3248299 (diff)
next commit
Diffstat (limited to 'player.gd')
-rw-r--r--player.gd154
1 files changed, 116 insertions, 38 deletions
diff --git a/player.gd b/player.gd
index 8f78605..9ca3553 100644
--- a/player.gd
+++ b/player.gd
@@ -2,11 +2,16 @@ class_name Player
extends CharacterBody2D
-const SPEED = 80.0
+signal hp_changed
+
+
+const MAX_SPEED = 80.0
+const ACCELERATION = MAX_SPEED * 0.1
+const DECELERATION = ACCELERATION
const JUMP_VELOCITY = -200.0
-const AIR_SPEED = SPEED * 0.75
+const MAX_AIR_SPEED = MAX_SPEED * 0.75
const FLY_VELOCITY = -80
-const HURT_VELOCITY = SPEED * 5
+const HURT_VELOCITY = MAX_SPEED * 5
const MAX_GRAVITY = 300.0
const MAX_GRAVITY_AIR = 100.0
@@ -22,12 +27,11 @@ var current_state: Dictionary = state_idle():
new_state.enter.call()
current_state = new_state
-var max_hp = 2
+var max_hp = 6
var hp = max_hp :
set(value):
- hp = value
- get_tree().current_scene.get_node("CanvasLayer/PanelContainer/Label").text = "HP: " + str(hp)
-
+ hp = clamp(value, 0, max_hp)
+ hp_changed.emit()
var sprite_small_star_base: Sprite2D
var air_shot_base: Area2D
var star_shot_base: Area2D
@@ -66,15 +70,20 @@ func _physics_process(delta: float) -> void:
current_state.process.call(delta)
-func apply_directional_movement(speed = SPEED):
+func apply_directional_movement(max_speed = MAX_SPEED):
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
- last_direction = direction
if direction < 0:
$AnimatedSprite2D.flip_h = true
else:
$AnimatedSprite2D.flip_h = false
- velocity.x = direction * speed
+
+ if sign(direction) != sign(last_direction):
+ velocity.x = 0
+
+ velocity.x = move_toward(velocity.x, direction * max_speed, ACCELERATION)
+
+ last_direction = direction
return direction
@@ -122,7 +131,7 @@ func air_shot(direction: float):
0.3
).set_ease(Tween.EASE_OUT)
tween.tween_callback(func():
- await get_tree().create_timer(0.3).timeout
+ await get_tree().create_timer(0.2).timeout
shot.queue_free()
)
@@ -137,20 +146,27 @@ func star_shot(direction: float):
var tween = get_tree().create_tween()
shot.body_entered.connect(func(body):
+ Game.score += randi_range(100, 1000)
%ShotObstacleHit.play()
tween.stop()
shot.queue_free()
body.queue_free()
)
+ shot.get_node("StarShotVisibleNotifier").screen_exited.connect(func():
+ if is_instance_valid(shot):
+ tween.stop()
+ shot.queue_free()
+ )
+
tween.tween_property(
shot,
"global_position",
+ #shot.global_position + Vector2(sign(direction) * abs(shot.global_position.x - get_viewport_rect().end.x), 0),
shot.global_position + Vector2(sign(direction) * get_viewport_rect().size.x, 0),
0.5
)
tween.tween_callback(func():
- await get_tree().create_timer(0.3).timeout
if is_instance_valid(shot):
shot.queue_free()
)
@@ -170,6 +186,8 @@ func _on_hurt_area_body_entered(body: Node2D) -> void:
if body.is_in_group("enemy"):
current_state.id = "" # to force trigger state change into hurt
current_state = state_hurt({"collider": body})
+ elif body is TileMapLayer:
+ current_state = state_ko()
func state_idle():
@@ -180,7 +198,7 @@ func state_idle():
$AnimatedSprite2D.play("idle")
,
"process": func(_delta):
- velocity.x = move_toward(velocity.x, 0, SPEED)
+ velocity.x = move_toward(velocity.x, 0, DECELERATION)
move_and_slide()
var direction = Input.get_axis("ui_left", "ui_right")
@@ -218,7 +236,10 @@ func state_walk():
if direction:
move_and_slide()
else:
- current_state = state_idle()
+ velocity.x = move_toward(velocity.x, 0, DECELERATION)
+ move_and_slide()
+ if velocity.x == 0:
+ current_state = state_idle()
if check_door_enter():
current_state = state_enter_door()
@@ -293,7 +314,7 @@ func state_fall():
if velocity.y >= MAX_GRAVITY:
$AnimatedSprite2D.play("fall")
- apply_directional_movement(AIR_SPEED)
+ apply_directional_movement(MAX_AIR_SPEED)
move_and_slide()
if is_on_floor():
@@ -343,7 +364,7 @@ func state_fly_idle():
$AnimatedSprite2D.play("fly")
%SoundFly.play()
- velocity.x = move_toward(velocity.x, 0, SPEED)
+ velocity.x = move_toward(velocity.x, 0, DECELERATION)
move_and_slide()
var direction = Input.get_axis("ui_left", "ui_right")
@@ -377,7 +398,7 @@ func state_fly_walk():
velocity.y = FLY_VELOCITY
%SoundFly.play()
- var direction = apply_directional_movement(AIR_SPEED)
+ var direction = apply_directional_movement(MAX_AIR_SPEED)
if direction:
$AnimatedSprite2D.play("fly")
move_and_slide()
@@ -425,7 +446,7 @@ func state_inhale():
%InhaleParticles.emitting = true
,
"process": func(_delta):
- velocity.x = move_toward(velocity.x, 0, SPEED)
+ velocity.x = move_toward(velocity.x, 0, DECELERATION)
move_and_slide()
var bodies = %InhaleArea.get_overlapping_bodies()
@@ -433,6 +454,7 @@ func state_inhale():
does_inhale_transition = true
for body in bodies:
body.queue_free()
+ Game.score += randi_range(10, 100)
# todo: stop process? -> remove collision -> inhale aka move towards center player -> queue_free
await get_tree().create_timer(inhale_min_duration).timeout
current_state = state_inflated_idle()
@@ -460,7 +482,7 @@ func state_inflated_idle():
$AnimatedSprite2D.play("inflated_idle")
,
"process": func(_delta):
- velocity.x = move_toward(velocity.x, 0, SPEED)
+ velocity.x = move_toward(velocity.x, 0, DECELERATION)
move_and_slide()
var direction = Input.get_axis("ui_left", "ui_right")
@@ -513,7 +535,7 @@ func state_inflated_walk():
current_state = state_inflated_fall()
if Input.is_action_pressed("duck"):
- current_state = state_duck()
+ current_state = state_digest()
if Input.is_action_just_pressed("inhale_exhale"):
star_shot(last_direction)
@@ -568,7 +590,7 @@ func state_inflated_fall():
star_shot(last_direction)
current_state = state_fall()
- apply_directional_movement(AIR_SPEED)
+ apply_directional_movement(MAX_AIR_SPEED)
move_and_slide()
if is_on_floor():
@@ -588,6 +610,8 @@ func state_digest():
"enter": func():
%SoundDigest.play()
is_inflated = false
+ hp += 1
+ Game.score += randi_range(10, 100)
,
"process": func(_delta):
current_state = state_duck()
@@ -607,8 +631,8 @@ func state_hurt(data):
hp -= 1
if hp == 0:
- %SoundNoHp.play()
- #current_state = state_game_over()
+ current_state = state_ko()
+ return
else:
%SoundHurt.play()
@@ -649,26 +673,80 @@ func state_enter_door():
return {
"id": "enter_door",
"enter": func():
- SoundManager.play("EnterDoor")
+ SoundManager.play_sound("EnterDoor")
$AnimatedSprite2D.play("enter_door")
- # TODO
- var canvas_layer = CanvasLayer.new()
- var color_rect = ColorRect.new()
- color_rect.size = get_viewport_rect().size
- color_rect.color = Color(1,1,1,0)
- canvas_layer.add_child(color_rect)
- get_tree().current_scene.add_child(canvas_layer)
var tween = get_tree().create_tween()
+ tween.tween_property($AnimatedSprite2D, "self_modulate", Color(0,0,0,1), 0.3)
+ await get_tree().create_timer(0.1).timeout
+ Game.transition_scene_with_door(entered_door)
+ ,
+ "process": func(_delta):
+ pass
+ ,
+ "exit": func():
+ pass
+ ,
+ }
+
+func state_exit_door():
+ return {
+ "id": "exit_door",
+ "enter": func():
+ $AnimatedSprite2D.play("exit_door")
+ await get_tree().create_timer(0.3).timeout
+ current_state = state_idle()
+ ,
+ "process": func(_delta):
+ pass
+ ,
+ "exit": func():
+ pass
+ ,
+ }
+
+func state_ko():
+ return {
+ "id": "ko",
+ "enter": func():
+ SoundManager.current_background.stop()
+
+ $AnimatedSprite2D.play("ko")
+ $AnimatedSprite2D.stop()
+ $AnimatedSprite2D.set_frame_and_progress(0, 0)
+
+ %SoundNoHp.play()
+ await %SoundNoHp.finished
+
+ $AnimatedSprite2D.play("ko")
+ $Camera2D.drag_top_margin = 1.0
+ var tween = get_tree().create_tween()
+ var to_up_difference = global_position.y - 16
+ var to_down_difference = global_position.y + get_viewport_rect().size.y
tween.tween_property(
- color_rect,
- "color",
- Color(1,1,1,1),
- 0.3
+ self,
+ "global_position",
+ Vector2(
+ global_position.x,
+ to_up_difference
+ ),
+ (to_up_difference / to_down_difference) * 1.0
)
- tween.tween_callback(func():
- $AnimatedSprite2D.play("exit_door")
- Game.transition_scene_with_door(entered_door)
+ tween.tween_property(
+ self,
+ "global_position",
+ Vector2(
+ global_position.x,
+ to_down_difference
+ ),
+ 1.0
+ )
+
+ %SoundKO.play()
+ await %SoundKO.finished
+
+ Game.transition_to_scene(
+ get_tree().current_scene.scene_file_path.replace(".tscn", "Intro.tscn")
)
,
"process": func(_delta):