Implement action nodes
parent
e7889a7a65
commit
7e12cfd408
@ -0,0 +1,7 @@
|
||||
extends Node
|
||||
class_name Action
|
||||
|
||||
var action_id = -1
|
||||
|
||||
func process(actor, delta):
|
||||
actor.setState(action_id)
|
@ -0,0 +1,10 @@
|
||||
extends Area2D
|
||||
class_name Actor
|
||||
|
||||
var state = 0
|
||||
var health = 100
|
||||
var state_map = {} # {STATE, Action Node}
|
||||
export var speed = 200 # How fast the actor will move (pixels/sec).
|
||||
|
||||
func setState(new_state):
|
||||
state = new_state
|
@ -0,0 +1,16 @@
|
||||
extends Node
|
||||
|
||||
|
||||
# Declare member variables here. Examples:
|
||||
# var a = 2
|
||||
# var b = "text"
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
#func _process(delta):
|
||||
# pass
|
@ -0,0 +1,22 @@
|
||||
extends Node
|
||||
class_name PlayerAttack
|
||||
|
||||
func run(actor, delta):
|
||||
|
||||
actor.getAnimatedSprite().animation = "slash"
|
||||
actor.getAnimatedSprite().speed_scale = 4
|
||||
var velocity = actor.last_process_velocity
|
||||
velocity = velocity.normalized() * actor.speed
|
||||
if velocity.length() > 0:
|
||||
actor.getAnimatedSprite().play()
|
||||
actor.position += velocity * delta * 0.5
|
||||
actor.position.x = clamp(actor.position.x, 0, actor.get_viewport_rect().size.x)
|
||||
actor.position.y = clamp(actor.position.y, 0, actor.get_viewport_rect().size.y)
|
||||
actor.getAnimatedSprite().flip_h = velocity.x < 0
|
||||
|
||||
|
||||
func spriteFinished(actor):
|
||||
|
||||
actor.state = actor.PlayerState.NORMAL
|
||||
actor.getAnimatedSprite().flip_h = false
|
||||
actor.getAnimatedSprite().speed_scale = 1
|
@ -0,0 +1,22 @@
|
||||
extends Node
|
||||
class_name PlayerDash
|
||||
|
||||
func run(actor, delta):
|
||||
|
||||
actor.getAnimatedSprite().animation = "dash"
|
||||
actor.getAnimatedSprite().speed_scale = 2.5
|
||||
var velocity = actor.last_process_velocity
|
||||
velocity = velocity.normalized() * actor.speed
|
||||
if velocity.length() > 0:
|
||||
actor.getAnimatedSprite().play()
|
||||
actor.position += velocity * delta * 1.8
|
||||
actor.position.x = clamp(actor.position.x, 0, actor.get_viewport_rect().size.x)
|
||||
actor.position.y = clamp(actor.position.y, 0, actor.get_viewport_rect().size.y)
|
||||
actor.getAnimatedSprite().flip_h = velocity.x < 0
|
||||
|
||||
|
||||
func spriteFinished(actor):
|
||||
|
||||
actor.state = actor.PlayerState.STANDING
|
||||
actor.getAnimatedSprite().flip_h = false
|
||||
actor.getAnimatedSprite().speed_scale = 1
|
@ -0,0 +1,37 @@
|
||||
extends Node
|
||||
class_name PlayerNormal
|
||||
|
||||
func run(actor, delta):
|
||||
|
||||
var velocity = Vector2() # The player's movement vector.
|
||||
if Input.is_key_pressed(KEY_SHIFT):
|
||||
actor.setState(actor.PlayerState.DASHING)
|
||||
if Input.is_key_pressed(KEY_Z):
|
||||
actor.setState(actor.PlayerState.ATTACKING)
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
velocity.x += 1
|
||||
if Input.is_action_pressed("ui_left"):
|
||||
velocity.x -= 1
|
||||
if Input.is_action_pressed("ui_down"):
|
||||
velocity.y += 1
|
||||
if Input.is_action_pressed("ui_up"):
|
||||
velocity.y -= 1
|
||||
if velocity.length() > 0:
|
||||
velocity = velocity.normalized() * actor.speed
|
||||
actor.getAnimatedSprite().play()
|
||||
|
||||
actor.position += velocity * delta
|
||||
actor.position.x = clamp(actor.position.x, 0, actor.get_viewport_rect().size.x)
|
||||
actor.position.y = clamp(actor.position.y, 0, actor.get_viewport_rect().size.y)
|
||||
|
||||
if velocity.x > 0 or velocity.y != 0:
|
||||
actor.getAnimatedSprite().animation = "forward"
|
||||
elif velocity.x < 0:
|
||||
actor.getAnimatedSprite().animation = "backward"
|
||||
else:
|
||||
actor.getAnimatedSprite().animation = "idle"
|
||||
|
||||
actor.last_process_velocity = velocity
|
||||
|
||||
func spriteFinished(actor):
|
||||
pass
|
@ -0,0 +1,17 @@
|
||||
extends Node
|
||||
class_name PlayerStanding
|
||||
|
||||
func run(actor, delta):
|
||||
|
||||
actor.getAnimatedSprite().animation = "standing up"
|
||||
actor.getAnimatedSprite().speed_scale = 1
|
||||
var velocity = Vector2(0.0, 0.0)
|
||||
actor.position += velocity.normalized() * delta
|
||||
actor.position.x = clamp(actor.position.x, 0, actor.get_viewport_rect().size.x)
|
||||
actor.position.y = clamp(actor.position.y, 0, actor.get_viewport_rect().size.y)
|
||||
actor.getAnimatedSprite().flip_h = actor.last_process_velocity.x < 0
|
||||
|
||||
func spriteFinished(actor):
|
||||
|
||||
actor.state = actor.PlayerState.NORMAL
|
||||
actor.getAnimatedSprite().flip_h = false
|
@ -1,94 +1,29 @@
|
||||
extends Area2D
|
||||
extends Actor
|
||||
|
||||
enum PlayerState { NORMAL, DASHING, STANDING };
|
||||
enum PlayerDirection { RIGHT, LEFT, DOWN, UP };
|
||||
enum PlayerState { NORMAL = 0, # < default from Actor
|
||||
DASHING = 1,
|
||||
STANDING = 2,
|
||||
ATTACKING = 3 };
|
||||
|
||||
export var speed = 200 # How fast the player will move (pixels/sec).
|
||||
var screen_size # Size of the game window.
|
||||
var state = PlayerState.NORMAL;
|
||||
var direction = PlayerDirection.RIGHT;
|
||||
var last_process_velocity = Vector2()
|
||||
|
||||
func start(pos):
|
||||
position = pos
|
||||
state_map = {PlayerState.NORMAL: PlayerNormal.new(),
|
||||
PlayerState.DASHING: PlayerDash.new(),
|
||||
PlayerState.STANDING: PlayerStanding.new(),
|
||||
PlayerState.ATTACKING: PlayerAttack.new()}
|
||||
show()
|
||||
|
||||
func _ready():
|
||||
screen_size = get_viewport_rect().size
|
||||
|
||||
func processNormal(delta):
|
||||
var velocity = Vector2() # The player's movement vector.
|
||||
if Input.is_key_pressed(KEY_SHIFT):
|
||||
state = PlayerState.DASHING
|
||||
return
|
||||
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
velocity.x += 1
|
||||
direction = PlayerDirection.RIGHT;
|
||||
if Input.is_action_pressed("ui_left"):
|
||||
velocity.x -= 1
|
||||
direction = PlayerDirection.LEFT;
|
||||
if Input.is_action_pressed("ui_down"):
|
||||
velocity.y += 1
|
||||
direction = PlayerDirection.DOWN;
|
||||
if Input.is_action_pressed("ui_up"):
|
||||
velocity.y -= 1
|
||||
direction = PlayerDirection.UP;
|
||||
if velocity.length() > 0:
|
||||
velocity = velocity.normalized() * speed
|
||||
$AnimatedSprite.play()
|
||||
|
||||
position += velocity * delta
|
||||
position.x = clamp(position.x, 0, screen_size.x)
|
||||
position.y = clamp(position.y, 0, screen_size.y)
|
||||
|
||||
if velocity.x > 0 or velocity.y != 0:
|
||||
$AnimatedSprite.animation = "forward"
|
||||
direction = PlayerDirection.RIGHT;
|
||||
elif velocity.x < 0:
|
||||
$AnimatedSprite.animation = "backward"
|
||||
direction = PlayerDirection.LEFT;
|
||||
else:
|
||||
$AnimatedSprite.animation = "idle"
|
||||
|
||||
last_process_velocity = velocity
|
||||
|
||||
func processDash(delta):
|
||||
$AnimatedSprite.animation = "dash"
|
||||
$AnimatedSprite.speed_scale = 2.5
|
||||
var velocity = last_process_velocity
|
||||
velocity = velocity.normalized() * speed
|
||||
if velocity.length() > 0:
|
||||
velocity = velocity.normalized() * speed
|
||||
$AnimatedSprite.play()
|
||||
position += velocity * delta * 1.8
|
||||
position.x = clamp(position.x, 0, screen_size.x)
|
||||
position.y = clamp(position.y, 0, screen_size.y)
|
||||
$AnimatedSprite.flip_h = velocity.x < 0
|
||||
|
||||
func processStanding(delta):
|
||||
$AnimatedSprite.animation = "standing up"
|
||||
$AnimatedSprite.speed_scale = 1
|
||||
var velocity = Vector2(0.0, 0.0)
|
||||
position += velocity.normalized() * delta
|
||||
position.x = clamp(position.x, 0, screen_size.x)
|
||||
position.y = clamp(position.y, 0, screen_size.y)
|
||||
$AnimatedSprite.flip_h = last_process_velocity.x < 0
|
||||
|
||||
func _process(delta):
|
||||
if state == PlayerState.NORMAL:
|
||||
processNormal(delta)
|
||||
elif state == PlayerState.DASHING:
|
||||
processDash(delta)
|
||||
else:
|
||||
processStanding(delta)
|
||||
|
||||
state_map[state].run(self, delta)
|
||||
|
||||
func _ready():
|
||||
screen_size = get_viewport_rect().size
|
||||
|
||||
func _on_AnimatedSprite_animation_finished():
|
||||
if $AnimatedSprite.animation == "dash": # Replace with function body.
|
||||
state = PlayerState.STANDING
|
||||
$AnimatedSprite.speed_scale = 1
|
||||
elif $AnimatedSprite.animation == "standing up":
|
||||
state = PlayerState.NORMAL
|
||||
$AnimatedSprite.flip_h = false
|
||||
state_map[state].spriteFinished(self)
|
||||
|
||||
func getAnimatedSprite():
|
||||
return $AnimatedSprite
|
||||
|
Loading…
Reference in New Issue