Implement action nodes

This commit is contained in:
NaiJi ✨ 2021-02-17 23:19:44 +03:00
parent e7889a7a65
commit 7e12cfd408
11 changed files with 232 additions and 102 deletions

7
Action.gd Normal file
View File

@ -0,0 +1,7 @@
extends Node
class_name Action
var action_id = -1
func process(actor, delta):
actor.setState(action_id)

10
Actor.gd Normal file
View File

@ -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

16
ActorProcess.gd Normal file
View File

@ -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

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=4 format=2] [gd_scene load_steps=5 format=2]
[ext_resource path="res://test-scene.tscn" type="PackedScene" id=1] [ext_resource path="res://test-scene.tscn" type="PackedScene" id=1]
[ext_resource path="res://Main.gd" type="Script" id=2] [ext_resource path="res://Main.gd" type="Script" id=2]
[ext_resource path="res://Actor.gd" type="Script" id=3]
[ext_resource path="res://Ground.tscn" type="PackedScene" id=4] [ext_resource path="res://Ground.tscn" type="PackedScene" id=4]
[node name="Main" type="Node"] [node name="Main" type="Node"]
@ -18,3 +19,6 @@ collision_mask = 0
[node name="StartPosition" type="Position2D" parent="."] [node name="StartPosition" type="Position2D" parent="."]
position = Vector2( 240, 450 ) position = Vector2( 240, 450 )
[node name="Actor" type="Area2D" parent="."]
script = ExtResource( 3 )

22
PlayerAttack.gd Normal file
View File

@ -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

22
PlayerDash.gd Normal file
View File

@ -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

37
PlayerNormal.gd Normal file
View File

@ -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

17
PlayerStanding.gd Normal file
View File

@ -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

View File

@ -1,94 +1,29 @@
extends Area2D extends Actor
enum PlayerState { NORMAL, DASHING, STANDING }; enum PlayerState { NORMAL = 0, # < default from Actor
enum PlayerDirection { RIGHT, LEFT, DOWN, UP }; 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 screen_size # Size of the game window.
var state = PlayerState.NORMAL;
var direction = PlayerDirection.RIGHT;
var last_process_velocity = Vector2() var last_process_velocity = Vector2()
func start(pos): func start(pos):
position = pos position = pos
state_map = {PlayerState.NORMAL: PlayerNormal.new(),
PlayerState.DASHING: PlayerDash.new(),
PlayerState.STANDING: PlayerStanding.new(),
PlayerState.ATTACKING: PlayerAttack.new()}
show() show()
func _process(delta):
state_map[state].run(self, delta)
func _ready(): func _ready():
screen_size = get_viewport_rect().size 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)
func _on_AnimatedSprite_animation_finished(): func _on_AnimatedSprite_animation_finished():
if $AnimatedSprite.animation == "dash": # Replace with function body. state_map[state].spriteFinished(self)
state = PlayerState.STANDING
$AnimatedSprite.speed_scale = 1 func getAnimatedSprite():
elif $AnimatedSprite.animation == "standing up": return $AnimatedSprite
state = PlayerState.NORMAL
$AnimatedSprite.flip_h = false

View File

@ -8,9 +8,44 @@
config_version=4 config_version=4
_global_script_classes=[ ] _global_script_classes=[ {
"base": "Node",
"class": "Action",
"language": "GDScript",
"path": "res://Action.gd"
}, {
"base": "Area2D",
"class": "Actor",
"language": "GDScript",
"path": "res://Actor.gd"
}, {
"base": "Node",
"class": "PlayerAttack",
"language": "GDScript",
"path": "res://PlayerAttack.gd"
}, {
"base": "Node",
"class": "PlayerDash",
"language": "GDScript",
"path": "res://PlayerDash.gd"
}, {
"base": "Node",
"class": "PlayerNormal",
"language": "GDScript",
"path": "res://PlayerNormal.gd"
}, {
"base": "Node",
"class": "PlayerStanding",
"language": "GDScript",
"path": "res://PlayerStanding.gd"
} ]
_global_script_class_icons={ _global_script_class_icons={
"Action": "",
"Actor": "",
"PlayerAttack": "",
"PlayerDash": "",
"PlayerNormal": "",
"PlayerStanding": ""
} }
[application] [application]

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=25 format=2] [gd_scene load_steps=33 format=2]
[ext_resource path="res://assets/art/forward-sheet.png" type="Texture" id=1] [ext_resource path="res://assets/art/forward-sheet.png" type="Texture" id=1]
[ext_resource path="res://assets/art/backwards-sheet.png" type="Texture" id=2] [ext_resource path="res://assets/art/backwards-sheet.png" type="Texture" id=2]
@ -13,65 +13,78 @@
[ext_resource path="res://assets/art/dash/dash-07.png" type="Texture" id=11] [ext_resource path="res://assets/art/dash/dash-07.png" type="Texture" id=11]
[ext_resource path="res://assets/art/dash/dash-03.png" type="Texture" id=12] [ext_resource path="res://assets/art/dash/dash-03.png" type="Texture" id=12]
[ext_resource path="res://assets/art/dash/dash-08.png" type="Texture" id=13] [ext_resource path="res://assets/art/dash/dash-08.png" type="Texture" id=13]
[ext_resource path="res://assets/art/slash/slash-02.png" type="Texture" id=14]
[ext_resource path="res://assets/art/slash/slash-03.png" type="Texture" id=15]
[ext_resource path="res://assets/art/slash/slash-01.png" type="Texture" id=16]
[ext_resource path="res://PlayerNormal.gd" type="Script" id=17]
[ext_resource path="res://assets/art/slash/slash-04.png" type="Texture" id=18]
[ext_resource path="res://PlayerDash.gd" type="Script" id=19]
[ext_resource path="res://PlayerStanding.gd" type="Script" id=20]
[ext_resource path="res://PlayerAttack.gd" type="Script" id=21]
[sub_resource type="AtlasTexture" id=9] [sub_resource type="AtlasTexture" id=1]
flags = 4 flags = 4
atlas = ExtResource( 3 ) atlas = ExtResource( 3 )
region = Rect2( 0, 0, 45, 47 ) region = Rect2( 0, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=10] [sub_resource type="AtlasTexture" id=2]
flags = 4 flags = 4
atlas = ExtResource( 3 ) atlas = ExtResource( 3 )
region = Rect2( 45, 0, 45, 47 ) region = Rect2( 45, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=11] [sub_resource type="AtlasTexture" id=3]
flags = 4 flags = 4
atlas = ExtResource( 3 ) atlas = ExtResource( 3 )
region = Rect2( 90, 0, 45, 47 ) region = Rect2( 90, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=12] [sub_resource type="AtlasTexture" id=4]
flags = 4 flags = 4
atlas = ExtResource( 3 ) atlas = ExtResource( 3 )
region = Rect2( 135, 0, 45, 47 ) region = Rect2( 135, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=13] [sub_resource type="AtlasTexture" id=5]
flags = 4 flags = 4
atlas = ExtResource( 1 ) atlas = ExtResource( 1 )
region = Rect2( 0, 0, 45, 47 ) region = Rect2( 0, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=14] [sub_resource type="AtlasTexture" id=6]
flags = 4 flags = 4
atlas = ExtResource( 1 ) atlas = ExtResource( 1 )
region = Rect2( 45, 0, 45, 47 ) region = Rect2( 45, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=15] [sub_resource type="AtlasTexture" id=7]
flags = 4 flags = 4
atlas = ExtResource( 1 ) atlas = ExtResource( 1 )
region = Rect2( 90, 0, 45, 47 ) region = Rect2( 90, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=5] [sub_resource type="AtlasTexture" id=8]
flags = 4 flags = 4
atlas = ExtResource( 2 ) atlas = ExtResource( 2 )
region = Rect2( 0, 0, 44, 47 ) region = Rect2( 0, 0, 44, 47 )
[sub_resource type="AtlasTexture" id=6] [sub_resource type="AtlasTexture" id=9]
flags = 4 flags = 4
atlas = ExtResource( 2 ) atlas = ExtResource( 2 )
region = Rect2( 44, 0, 44, 47 ) region = Rect2( 44, 0, 44, 47 )
[sub_resource type="AtlasTexture" id=7] [sub_resource type="AtlasTexture" id=10]
flags = 4 flags = 4
atlas = ExtResource( 2 ) atlas = ExtResource( 2 )
region = Rect2( 88, 0, 44, 47 ) region = Rect2( 88, 0, 44, 47 )
[sub_resource type="SpriteFrames" id=8] [sub_resource type="SpriteFrames" id=11]
animations = [ { animations = [ {
"frames": [ SubResource( 9 ), SubResource( 10 ), SubResource( 11 ), SubResource( 12 ) ], "frames": [ SubResource( 1 ), SubResource( 2 ), SubResource( 3 ), SubResource( 4 ) ],
"loop": true, "loop": true,
"name": "idle", "name": "idle",
"speed": 5.0 "speed": 5.0
}, { }, {
"frames": [ SubResource( 13 ), SubResource( 14 ), SubResource( 15 ) ], "frames": [ ExtResource( 16 ), ExtResource( 14 ), ExtResource( 15 ), ExtResource( 18 ) ],
"loop": true,
"name": "slash",
"speed": 5.0
}, {
"frames": [ SubResource( 5 ), SubResource( 6 ), SubResource( 7 ) ],
"loop": true, "loop": true,
"name": "forward", "name": "forward",
"speed": 5.0 "speed": 5.0
@ -81,7 +94,7 @@ animations = [ {
"name": "dash", "name": "dash",
"speed": 5.0 "speed": 5.0
}, { }, {
"frames": [ SubResource( 5 ), SubResource( 6 ), SubResource( 7 ) ], "frames": [ SubResource( 8 ), SubResource( 9 ), SubResource( 10 ) ],
"loop": true, "loop": true,
"name": "backward", "name": "backward",
"speed": 5.0 "speed": 5.0
@ -99,9 +112,21 @@ __meta__ = {
} }
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 8 ) frames = SubResource( 11 )
animation = "standing up" animation = "slash"
frame = 1 frame = 2
playing = true playing = true
[node name="PlayerNormal" type="Node" parent="."]
script = ExtResource( 17 )
[node name="PlayerDash" type="Node" parent="."]
script = ExtResource( 19 )
[node name="PlayerStanding" type="Node" parent="."]
script = ExtResource( 20 )
[node name="PlayerAttack" type="Node" parent="."]
script = ExtResource( 21 )
[connection signal="body_entered" from="." to="." method="_on_Player_body_entered"] [connection signal="body_entered" from="." to="." method="_on_Player_body_entered"]
[connection signal="animation_finished" from="AnimatedSprite" to="." method="_on_AnimatedSprite_animation_finished"] [connection signal="animation_finished" from="AnimatedSprite" to="." method="_on_AnimatedSprite_animation_finished"]