Heck yeah, you punch those trees, Gary!
This commit is contained in:
36
character/character.gd
Normal file
36
character/character.gd
Normal file
@@ -0,0 +1,36 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
var _grid_pos: Vector2i = Vector2i.ZERO
|
||||
var _grid_tween: Tween = null
|
||||
|
||||
var _cached_dir: Vector2i = Vector2i.ZERO #used for reversing the current input
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
#WARNING: this has the potential for getting stuck
|
||||
if _grid_pos * 32 == Vector2i(position):
|
||||
_check_grid_move(delta)
|
||||
|
||||
func _check_grid_move(delta: float) -> void:
|
||||
if Input.is_action_pressed("input_north"):
|
||||
_set_grid_move(Vector2i.UP)
|
||||
elif Input.is_action_pressed("input_south"):
|
||||
_set_grid_move(Vector2i.DOWN)
|
||||
elif Input.is_action_pressed("input_west"):
|
||||
_set_grid_move(Vector2i.LEFT)
|
||||
elif Input.is_action_pressed("input_east"):
|
||||
_set_grid_move(Vector2i.RIGHT)
|
||||
|
||||
func _set_grid_move(grid_dir: Vector2i):
|
||||
_cached_dir = grid_dir
|
||||
_grid_pos += grid_dir
|
||||
_grid_tween = create_tween()
|
||||
_grid_tween.set_ease(Tween.EASE_OUT_IN)
|
||||
_grid_tween.set_trans(Tween.TRANS_QUAD)
|
||||
_grid_tween.tween_property(self, "position", Vector2(_grid_pos * 32), 0.3)
|
||||
|
||||
func repel_collision(_other: Node2D) -> void:
|
||||
#NOTE: character's collider has a 1 pixel margin
|
||||
if _cached_dir != Vector2i.ZERO:
|
||||
_grid_tween.kill()
|
||||
_set_grid_move(-_cached_dir) #go backwards
|
||||
_cached_dir = Vector2i.ZERO #only do this once each collision
|
||||
Reference in New Issue
Block a user