Skip to content

Instantly share code, notes, and snippets.

@alexmackenzie-wx
Created November 15, 2022 14:08
Show Gist options
  • Save alexmackenzie-wx/83342ca1f5074750ffe529ef37c1710f to your computer and use it in GitHub Desktop.
Save alexmackenzie-wx/83342ca1f5074750ffe529ef37c1710f to your computer and use it in GitHub Desktop.
extends Area2D
# Signals
signal pickup
signal hurt
# Variables
export (int) var speed # Property in inspector
var velocity = Vector2()
var screensize = Vector2(480, 720)
# Process Functions
func get_input():
velocity = Vector2()
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
# Process Function
func _process(delta):
get_input()
position += velocity * delta
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.y)
if velocity.length() > 0:
$AnimatedSprite.animation = "run"
$AnimatedSprite.flip_h = velocity.x < 0
else:
$AnimatedSprite.animation = "idle"
func start(pos):
set_process(true)
position = pos
$AnimatedSprite.animation = "idle"
func die():
$AnimatedSprite.animation = "hurt"
set_process(false)
func _on_Player_area_entered(area):
if area.is_in_group("coins"):
area.pickup()
emit_signal("pickup")
if area.is_in_group("obstacles"):
emit_signal("hurt")
die()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment