105 lines
2.3 KiB
GDScript
105 lines
2.3 KiB
GDScript
extends CharacterBody2D
|
|
@onready var animated_sprite_2d = $AnimatedSprite2D
|
|
@onready var camera_2d = $Camera2D
|
|
@onready var color_rect = $Camera2D/ColorRect
|
|
@onready var pipe_timer = $pipe_timer
|
|
@onready var pipe_timer_half = $pipe_timer_half
|
|
@onready var coin_amt = $CanvasLayer/Control/CoinAmt
|
|
|
|
|
|
const SPEED = 130.0
|
|
const JUMP_VELOCITY = -360.0
|
|
var on_pipe = false
|
|
var right_pipe = false
|
|
var left_pipe = false
|
|
var pipe = null
|
|
var pipe_target = null
|
|
|
|
var coins = 0
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
|
var on_pause = false
|
|
|
|
func _ready():
|
|
color_rect.visible = false
|
|
|
|
func _process(delta):
|
|
# set camera
|
|
if position.y <= 32:
|
|
camera_2d.limit_bottom = 32
|
|
camera_2d.limit_top = -1000000
|
|
else:
|
|
camera_2d.limit_bottom = 272
|
|
camera_2d.limit_top = 32
|
|
|
|
# check pipe
|
|
if on_pipe and Input.is_action_just_pressed("crawl") and is_on_floor() or right_pipe and Input.is_action_pressed("move_right"):
|
|
color_rect.visible = true
|
|
pipe_timer.start()
|
|
pipe_timer_half.start()
|
|
pipe_target = pipe.get_target()
|
|
|
|
func _physics_process(delta):
|
|
# Add the gravity.
|
|
if not is_on_floor():
|
|
velocity.y += gravity * delta
|
|
|
|
# Handle jump.
|
|
if not on_pause and Input.is_action_just_pressed("jump") and is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
# get input
|
|
var direction = Input.get_axis("move_left", "move_right")
|
|
|
|
if on_pause:
|
|
direction = 0
|
|
|
|
# face direction
|
|
if direction < 0:
|
|
animated_sprite_2d.flip_h = true
|
|
elif direction > 0:
|
|
animated_sprite_2d.flip_h = false
|
|
|
|
# play animation
|
|
if is_on_floor():
|
|
if direction != 0:
|
|
animated_sprite_2d.play("run")
|
|
else:
|
|
animated_sprite_2d.play("idle")
|
|
else:
|
|
animated_sprite_2d.play("jump")
|
|
|
|
if direction:
|
|
velocity.x = direction * SPEED
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
move_and_slide()
|
|
|
|
func increase_coins():
|
|
coins += 1
|
|
coin_amt.text = str(coins)
|
|
|
|
func _on_pipedetector_area_entered(area):
|
|
on_pipe = true
|
|
pipe = area
|
|
|
|
func _on_pipedetector_area_exited(area):
|
|
on_pipe = false
|
|
|
|
func _on_pipedetector_right_area_entered(area):
|
|
right_pipe = true
|
|
pipe = area
|
|
|
|
func _on_pipedetector_right_area_exited(area):
|
|
right_pipe = false
|
|
|
|
|
|
func _on_pipe_timer_timeout():
|
|
on_pause = false
|
|
color_rect.visible = false
|
|
|
|
|
|
func _on_pipe_timer_half_timeout():
|
|
position = pipe_target
|