Skip to content

Instantly share code, notes, and snippets.

@jcreager jcreager/character.gd Secret
Last active Aug 22, 2019

Embed
What would you like to do?
Godot Character Movement and Animation
extends KinematicBody2D
var idle_timeout = 100 setget set_idle_timeout
var idle_cycles = 5 setget set_idle_cycles
var walking_timeout = 0 setget set_walking_timeout
var player_direction = "down" setget set_player_direction
var start_frames = {}
signal walking
signal idle
func set_idle_timeout(new_value):
idle_timeout = new_value
pass
func set_idle_cycles(new_value):
idle_cycles = new_value
pass
func set_walking_timeout(new_value):
walking_timeout = new_value
pass
func set_player_direction(new_direction):
player_direction = new_direction
pass
func _ready():
set_process(true)
self.connect("walking", self, "_on_walking")
self.connect("idle", self, "_on_idle")
start_frames.walking = init_start_frames($walking.vframes, $walking.hframes)
start_frames.idle = init_start_frames($idle.vframes, $idle.hframes)
pass
func _physics_process(delta):
var velocity_x = 0
var velocity_y = 0
var speed = 0
if walking_timeout > 0:
walking_timeout -= 1
if idle_timeout > 0:
idle_timeout -= 1
else:
emit_signal("idle")
if Input.is_action_pressed("player_left"):
velocity_x = -1
speed = 100
emit_signal("walking", "left")
if Input.is_action_pressed("player_right"):
velocity_x = 1
speed = 100
emit_signal("walking", "right")
if Input.is_action_pressed("player_up"):
velocity_y = -1
speed = 100
emit_signal("walking", "up")
if Input.is_action_pressed("player_down"):
velocity_y = 1
speed = 100
emit_signal("walking", "down")
move_and_slide(Vector2(velocity_x * speed, velocity_y * speed), Vector2(0, 1))
pass
func _on_walking(new_direction):
var start_frame
$idle.hide()
$walking.show()
player_direction = new_direction
if walking_timeout > 0: return
start_frame = start_frames.walking[player_direction]
$walking.frame = get_next_frame($walking.frame, start_frame, $walking.hframes)
walking_timeout = 5
idle_timeout = 100
if $walking.frame != start_frame:
idle_timeout = 5
pass
func _on_idle():
var start_frame
$walking.hide()
$idle.show()
if idle_timeout > 0: return
start_frame = start_frames.idle[player_direction]
$idle.frame = get_next_frame($idle.frame, start_frame, $idle.hframes)
idle_timeout = 5
idle_cycles += 1
if idle_cycles > 5 and $idle.frame >= start_frame + $idle.hframes - 1:
idle_timeout = 100
idle_cycles = 0
pass
func get_next_frame(frame, start, boundry):
if frame < start + boundry:
frame += 1
if frame >= start + boundry:
frame = start
if frame < start:
frame = start
return frame
pass
func get_start_frame(direction, vframes, hframes):
var frame
match direction:
"left":
frame = vframes * hframes - hframes
"right":
frame = vframes * hframes - hframes * 2
"up":
frame = vframes * hframes - hframes * 3
"down":
frame = vframes * hframes - hframes * 4
return frame
pass
func init_start_frames(vframes, hframes):
var directions = ["left", "right", "up", "down"]
var frames = {}
for i in directions.size():
frames[directions[i]] = get_start_frame(directions[i], vframes, hframes)
return frames
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.