235 lines
5.6 KiB
GDScript
235 lines
5.6 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
|
|
@onready var collision_shape_2d = $CollisionShape2D
|
|
@onready var pipedetector_ground = $pipedetector_ground
|
|
@onready var pipe_sound = $PipeSound
|
|
@onready var jump_sound = $JumpSound
|
|
@onready var shrink_sound = $shrinkSound
|
|
@onready var die_player = $diePlayer
|
|
@onready var music = %Music
|
|
@onready var timer_fire = $timer_fire
|
|
|
|
|
|
const SPEED = 130.0
|
|
var JUMPt_velocity = -360.0
|
|
var org_jump_veloc : int
|
|
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
|
|
var is_big = false
|
|
var is_flower = false
|
|
|
|
# touch controls
|
|
var touch_anchor = Vector2.ZERO
|
|
var jump = false
|
|
var crawl = false
|
|
var fire = false
|
|
var toucht_velocity = Vector2.ZERO
|
|
var t_velocity = 0
|
|
var can_fire = 0
|
|
|
|
@export var fireball_prefab : PackedScene
|
|
|
|
func _unhandled_input(event):
|
|
if event is InputEventScreenTouch or event is InputEventScreenDrag:
|
|
if event.is_pressed():
|
|
if event.position.x < get_viewport_rect().size.x / 2:
|
|
touch_anchor = event.position
|
|
else:
|
|
if event.position.y > get_viewport_rect().size.y / 2:
|
|
if not jump:
|
|
jump = true
|
|
elif not fire:
|
|
fire = true
|
|
elif event.position.x < get_viewport_rect().size.x / 2:
|
|
toucht_velocity = event.position - touch_anchor
|
|
if abs(toucht_velocity.x) > 30:
|
|
t_velocity = 1 if toucht_velocity.x > 0 else -1
|
|
else:
|
|
t_velocity = 0
|
|
|
|
if toucht_velocity.y > 30:
|
|
crawl = true
|
|
if event is InputEventScreenTouch and event.is_released():
|
|
if event.position.x < get_viewport_rect().size.x / 2:
|
|
toucht_velocity = Vector2.ZERO
|
|
t_velocity = 0
|
|
crawl = false
|
|
|
|
func _ready():
|
|
color_rect.visible = false
|
|
org_jump_veloc = JUMPt_velocity
|
|
pipedetector_ground.position.y = 0
|
|
collision_shape_2d.shape.height = 15
|
|
|
|
func _process(delta):
|
|
# set camera
|
|
if position.y <= 32:
|
|
camera_2d.limit_bottom = 32
|
|
camera_2d.limit_top = -1000
|
|
else:
|
|
camera_2d.limit_bottom = 272
|
|
camera_2d.limit_top = 32
|
|
|
|
# check pipe
|
|
if on_pipe and (Input.is_action_just_pressed("crawl") or crawl) and is_on_floor() or right_pipe and (Input.is_action_pressed("move_right") or t_velocity > 0):
|
|
pipe_sound.play()
|
|
crawl = false
|
|
on_pause = true
|
|
t_velocity = 0
|
|
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() and not on_pause:
|
|
velocity.y += gravity * delta
|
|
|
|
# Handle jump.
|
|
if not on_pause and (Input.is_action_pressed("jump") or jump) and is_on_floor():
|
|
jump_sound.play()
|
|
jump = false
|
|
velocity.y = JUMPt_velocity
|
|
|
|
# check fireball
|
|
if is_flower and (Input.is_action_just_pressed("flower") or fire) and can_fire < 2:
|
|
can_fire += 1
|
|
fire = false
|
|
var instance = fireball_prefab.instantiate()
|
|
get_parent().add_child(instance)
|
|
instance.position.x = position.x -8 if animated_sprite_2d.flip_h else position.x + 8
|
|
instance.position.y = position.y
|
|
instance.linear_velocity.x = -200 if animated_sprite_2d.flip_h else 200
|
|
instance.linear_velocity.y = 50
|
|
instance.angular_velocity = 0
|
|
timer_fire.start()
|
|
|
|
|
|
# get input
|
|
var direction = Input.get_axis("move_left", "move_right") + t_velocity
|
|
|
|
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:
|
|
if is_flower:
|
|
animated_sprite_2d.play("run_flower")
|
|
elif is_big:
|
|
animated_sprite_2d.play("run_big")
|
|
else:
|
|
animated_sprite_2d.play("run")
|
|
else:
|
|
if is_flower:
|
|
animated_sprite_2d.play("idle_flower")
|
|
elif is_big:
|
|
animated_sprite_2d.play("idle_big")
|
|
else:
|
|
animated_sprite_2d.play("idle")
|
|
else:
|
|
if is_flower:
|
|
animated_sprite_2d.play("jump_flower")
|
|
elif is_big:
|
|
animated_sprite_2d.play("jump_big")
|
|
else:
|
|
animated_sprite_2d.play("jump")
|
|
|
|
if direction:
|
|
velocity.x = direction * SPEED
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
if not on_pause:
|
|
move_and_slide()
|
|
|
|
func grow():
|
|
if not is_big:
|
|
is_big = true
|
|
if is_on_floor():
|
|
position.y -= 20
|
|
collision_shape_2d.shape.height = 31
|
|
pipedetector_ground.position.y += 8
|
|
|
|
func flower():
|
|
if not is_big:
|
|
collision_shape_2d.shape.height = 31
|
|
pipedetector_ground.position.y += 8
|
|
is_big = true
|
|
is_flower = true
|
|
|
|
func shrink():
|
|
if on_pause:
|
|
return
|
|
|
|
if is_flower:
|
|
is_flower = false
|
|
shrink_sound.play()
|
|
elif is_big:
|
|
is_big = false
|
|
collision_shape_2d.shape.height = 15
|
|
pipedetector_ground.position.y -= 8
|
|
shrink_sound.play()
|
|
else:
|
|
player_die()
|
|
|
|
func player_die():
|
|
die_player.play("die")
|
|
on_pause = true
|
|
music.stop()
|
|
|
|
func reload_scene():
|
|
get_tree().reload_current_scene()
|
|
|
|
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
|
|
pipe_sound.play()
|
|
|
|
|
|
func _on_pipe_timer_half_timeout():
|
|
position = pipe_target
|
|
|
|
|
|
func _on_timer_fire_timeout():
|
|
can_fire = 0
|