Files
retromariogodot/scripts/player.gd
T
2024-07-28 23:44:55 +02:00

138 lines
3.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 JUMPt_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
# touch controls
var touch_anchor = Vector2.ZERO
var jump = false
var crawl = false
var toucht_velocity = Vector2.ZERO
var t_velocity = 0
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
elif not jump:
jump = 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
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") or crawl) and is_on_floor() or right_pipe and (Input.is_action_pressed("move_right") or t_velocity > 0):
crawl = false
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():
velocity.y += gravity * delta
# Handle jump.
if not on_pause and (Input.is_action_just_pressed("jump") or jump) and is_on_floor():
jump = false
velocity.y = JUMPt_velocity
# 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:
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