mobile/android support

This commit is contained in:
Jonas Braus
2024-07-28 23:44:55 +02:00
parent 7a8a1cd61c
commit ec9ff7e446
10 changed files with 437 additions and 12 deletions
+4 -1
View File
@@ -1,5 +1,8 @@
extends Area2D
func _on_body_entered(body):
func collect_coin(body):
body.increase_coins()
queue_free()
func _on_body_entered(body):
collect_coin(body)
+38 -5
View File
@@ -8,7 +8,7 @@ extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -360.0
const JUMPt_velocity = -360.0
var on_pipe = false
var right_pipe = false
var left_pipe = false
@@ -20,6 +20,35 @@ var coins = 0
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
@@ -33,7 +62,9 @@ func _process(delta):
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"):
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()
@@ -45,11 +76,13 @@ func _physics_process(delta):
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
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")
var direction = Input.get_axis("move_left", "move_right") + t_velocity
if on_pause:
direction = 0