Cow_Life_Sim_RPG/MiniGames/FlappyCow/Player/FlappyCow.gd

101 lines
2.3 KiB
GDScript

extends CharacterBody2D
# BASIC MOVEMENT VARAIABLES ---------------- #
var speed := 500
# GRAVITY ----- #
@export var gravity_acceleration : float = 3840
@export var gravity_max : float = 1020
# JUMP VARAIABLES ------------------- #
@export var jump_force : float = 1400
@export var jump_cut : float = 0.25
@export var jump_gravity_max : float = 500
# All iputs we want to keep track of
func get_input() -> Dictionary:
return {
"jump": Input.is_action_just_pressed("jump") == true,
}
func _physics_process(delta: float) -> void:
x_movement(delta)
jump_logic(delta)
apply_gravity(delta)
move_and_slide()
func is_die():
return false
func x_movement(delta: float) -> void:
velocity.x = 50*speed*delta
#velocity.x = Vector2(velocity.x, 0).move_toward(Vector2(speed,0),1000*delta).x
func jump_logic(_delta: float) -> void:
# Reset our jump requirements
# Jump if grounded, there is jump input, and we aren't jumping already
if get_input()["jump"]:
$Sprite/AnimatedSprite2D.stop()
$Sprite/AnimatedSprite2D.play("default")
velocity.y = -jump_force
func apply_gravity(delta: float) -> void:
var applied_gravity : float = 0
# Normal gravity limit
if velocity.y <= gravity_max:
applied_gravity = gravity_acceleration * delta
# If moving upwards while jumping, the limit is jump_gravity_max to achieve lower gravity
if velocity.y > jump_gravity_max:
applied_gravity = 0
velocity.y += applied_gravity
func _on_area_2d_area_shape_entered(_area_rid, area, _area_shape_index, _local_shape_index):
if area.is_in_group("Death") and speed > 0:
Suspend()
get_node("Explosion").emitting = true
get_node("CanvasLayer/SpilledLabel").visible = true
var timeSurvived = get_parent().timePlayed
var xpReward = int(timeSurvived * 10)
await get_tree().create_timer(1).timeout
get_node("/root/MainGame/CanvasLayer/MessageZone").visible = true
get_node("/root/MainGame/CanvasLayer/MenuBar").visible = true
get_node("/root/MainGame/CanvasLayer/MinimizeMessageZoneButton").visible = true
get_tree().paused = false
LevelManager.add_XP("gaming", xpReward)
get_parent().queue_free() #exit on death
func _on_animated_sprite_2d_animation_looped():
$Sprite/AnimatedSprite2D.stop()
func Suspend():
speed = 0
$Camera2D/sun.speed = 0
gravity_acceleration = 0
velocity = Vector2(0,0)
jump_force = 0