Advertisement
Keyaku

Godot: 2D 8-direction character movement and animation

Jan 22nd, 2017
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. # Constants
  4. const MOTION_SPEED = 300 # Pixels/second
  5. enum {
  6.     SPR_UP = 1, SPR_DOWN = 2, SPR_LEFT = 4, SPR_RIGHT = 8,
  7. }
  8.  
  9. # Nodes
  10. onready var Anims = get_node("animations")
  11.  
  12. # "Private" members
  13. onready var sprite_direction = { # All 8 directions for a sprite's animation
  14.     SPR_UP               : "up"        ,
  15.     SPR_DOWN             : "down"      ,
  16.     SPR_LEFT             : "left"      ,
  17.     SPR_RIGHT            : "right"     ,
  18.     SPR_UP   | SPR_LEFT  : "up_left"   ,
  19.     SPR_UP   | SPR_RIGHT : "up_right"  ,
  20.     SPR_DOWN | SPR_LEFT  : "down_left" ,
  21.     SPR_DOWN | SPR_RIGHT : "down_right",
  22. }
  23. onready var sprite_motion = {
  24.     SPR_UP               : Vector2( 0, -1),
  25.     SPR_DOWN             : Vector2( 0,  1),
  26.     SPR_LEFT             : Vector2(-1,  0),
  27.     SPR_RIGHT            : Vector2( 1,  0),
  28.     SPR_UP   | SPR_LEFT  : Vector2(-1, -1),
  29.     SPR_UP   | SPR_RIGHT : Vector2( 1, -1),
  30.     SPR_DOWN | SPR_LEFT  : Vector2(-1,  1),
  31.     SPR_DOWN | SPR_RIGHT : Vector2( 1,  1),
  32. }
  33.  
  34. ######################
  35. ### Core functions ###
  36. ######################
  37. func _ready():
  38.     set_fixed_process(true)
  39.  
  40. func _fixed_process(delta):
  41.     # Grabbing directions from Input and transforming them into flags
  42.     var directions = int(Input.is_action_pressed("up"))    << 0
  43.     directions    |= int(Input.is_action_pressed("down"))  << 1
  44.     directions    |= int(Input.is_action_pressed("left"))  << 2
  45.     directions    |= int(Input.is_action_pressed("right")) << 3
  46.  
  47.     # If it can animate the character, then it can move
  48.     if animate_character(directions):
  49.         move_character(directions, delta)
  50.  
  51. ###############
  52. ### Methods ###
  53. ###############
  54. func animate_character(directions):
  55.     # If it can move to this direction
  56.     if sprite_direction.has(directions):
  57.         var new_anim = sprite_direction[directions]
  58.         if new_anim != Anims.get_current_animation() || !Anims.is_playing():
  59.             Anims.play(new_anim)
  60.     else:
  61.         Anims.stop()
  62.         # FIXME: Project-specific: make sure you set the sprite at the correct idle frame.
  63.  
  64.     return Anims.is_playing()
  65.  
  66. func move_character(directions, delta=0):
  67.     var motion = sprite_motion[directions]
  68.     motion = motion.normalized() * MOTION_SPEED * delta
  69.     motion = move(motion)
  70.  
  71.     # Make character slide when collisions are detected
  72.     var slide_attempts = 4
  73.     while is_colliding() and slide_attempts > 0:
  74.         motion = get_collision_normal().slide(motion)
  75.         motion = move(motion)
  76.         slide_attempts -= 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement