Improve actions and add attacking

This commit is contained in:
NaiJi ✨ 2021-03-02 20:27:36 +03:00
parent a4f3361048
commit c4e2b2eac4
16 changed files with 238 additions and 128 deletions

View File

@ -1,4 +1,4 @@
extends Area2D extends KinematicBody2D
class_name Actor class_name Actor
var state = 0 var state = 0

5
Area2D.gd Normal file
View File

@ -0,0 +1,5 @@
extends Area2D
func _ready():
var parent = get_parent()
pass

View File

@ -7,5 +7,6 @@ func register(actor):
return self return self
func trigger(actor): func trigger(actor):
actor.state = actor.PlayerState.DASHING if (actor.velocity != Vector2(0, 0)):
actor.state = actor.PlayerState.DASHING

11
Enemy.gd Normal file
View File

@ -0,0 +1,11 @@
extends Actor
class_name Enemy
func _ready():
$AnimatedSprite.flip_h = true
func on_collide(actor):
actor.health = 50 # DO PUSHING AWAY STATE!!!
func on_attack():
hide()

36
Enemy.tscn Normal file
View File

@ -0,0 +1,36 @@
[gd_scene load_steps=13 format=2]
[ext_resource path="res://Enemy.gd" type="Script" id=1]
[ext_resource path="res://assets/art/enemy-idle/idle-5.png" type="Texture" id=2]
[ext_resource path="res://assets/art/enemy-idle/idle-8.png" type="Texture" id=3]
[ext_resource path="res://assets/art/enemy-idle/idle-9.png" type="Texture" id=4]
[ext_resource path="res://assets/art/enemy-idle/idle-4.png" type="Texture" id=5]
[ext_resource path="res://assets/art/enemy-idle/idle-7.png" type="Texture" id=6]
[ext_resource path="res://assets/art/enemy-idle/idle-2.png" type="Texture" id=7]
[ext_resource path="res://assets/art/enemy-idle/idle-1.png" type="Texture" id=8]
[ext_resource path="res://assets/art/enemy-idle/idle-3.png" type="Texture" id=9]
[ext_resource path="res://assets/art/enemy-idle/idle-6.png" type="Texture" id=10]
[sub_resource type="SpriteFrames" id=1]
animations = [ {
"frames": [ ExtResource( 8 ), ExtResource( 7 ), ExtResource( 9 ), ExtResource( 5 ), ExtResource( 2 ), ExtResource( 10 ), ExtResource( 6 ), ExtResource( 3 ), ExtResource( 4 ) ],
"loop": true,
"name": "idle",
"speed": 10.0
} ]
[sub_resource type="CircleShape2D" id=2]
radius = 14.0651
[node name="Enemy" type="KinematicBody2D"]
script = ExtResource( 1 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 1 )
animation = "idle"
frame = 7
playing = true
offset = Vector2( 0, -10 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 2 )

View File

@ -1,9 +1,9 @@
extends Node extends Node
func _ready(): func _ready():
newGame() new_game()
func newGame(): func new_game():
var player = preload("res://Player.tscn").instance() var player = preload("res://Player.tscn").instance()
player.name = "Player" player.name = "Player"
player.start($StartPosition.position) player.start($StartPosition.position)
@ -15,3 +15,7 @@ func newGame():
orb.scale = Vector2(0.4, 0.4) orb.scale = Vector2(0.4, 0.4)
add_child(orb) add_child(orb)
var enemy = preload("res://Enemy.tscn").instance()
enemy.position.x = player.position.x + 220
enemy.position.y = player.position.y - 300
add_child(enemy)

View File

@ -14,5 +14,5 @@ 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="."] [node name="Actor" type="KinematicBody2D" parent="."]
script = ExtResource( 3 ) script = ExtResource( 3 )

View File

@ -1,7 +1,7 @@
extends Area2D extends StaticBody2D
func _on_OrbItem_area_shape_entered(area_id, area, area_shape, self_shape): func on_collide(actor):
if area.name == "Player": var dk = DashKey.new()
var dk = DashKey.new() var state = actor.PlayerState.NORMAL
area.key_map[KEY_SHIFT] = dk.register(area) actor.state_map[state].keymap[KEY_SHIFT] = dk.register(actor)
get_tree().queue_delete(self) get_tree().queue_delete(self)

View File

@ -6,8 +6,7 @@
[sub_resource type="CircleShape2D" id=1] [sub_resource type="CircleShape2D" id=1]
radius = 33.9555 radius = 33.9555
[node name="OrbItem" type="Area2D"] [node name="StaticBody2D" type="StaticBody2D"]
gravity = 0.0
script = ExtResource( 2 ) script = ExtResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
@ -26,4 +25,3 @@ text = "PICKUP TO DASH"
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[connection signal="area_shape_entered" from="." to="." method="_on_OrbItem_area_shape_entered"]

View File

@ -6,27 +6,28 @@ enum PlayerState { NORMAL = 0, # < default from Actor
ATTACKING = 3 }; ATTACKING = 3 };
var screen_size # Size of the game window. var screen_size # Size of the game window.
var last_process_velocity = Vector2() var velocity = Vector2()
var direction_vector = Vector2(1, 0) # to the right by default
var key_map
func start(pos): func start(pos):
position = pos position = pos
state_map = {PlayerState.NORMAL: PlayerNormal.new(), state_map = {PlayerState.NORMAL: PlayerNormal.new()}
PlayerState.ATTACKING: PlayerAttack.new()} state_map[PlayerState.NORMAL].ready(self)
var ak = AttackKey.new()
key_map = { KEY_Z: ak.register(self) }
show() show()
func _process(delta): func _process(delta):
state_map[state].run(self, delta) delta = 0
state_map[state].process(self)
func _physics_process(delta):
state_map[state].input(self)
state_map[state].physics_process(self, delta)
func _ready(): func _ready():
screen_size = get_viewport_rect().size screen_size = get_viewport_rect().size
func _on_AnimatedSprite_animation_finished(): func _on_AnimatedSprite_animation_finished():
state_map[state].spriteFinished(self) state_map[state].sprite_finished(self)
func getAnimatedSprite(): func get_animated_sprite():
return $AnimatedSprite return $AnimatedSprite

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=36 format=2] [gd_scene load_steps=37 format=2]
[ext_resource path="res://Player.gd" type="Script" id=1] [ext_resource path="res://Player.gd" type="Script" id=1]
[ext_resource path="res://DashKey.gd" type="Script" id=2] [ext_resource path="res://DashKey.gd" type="Script" id=2]
@ -24,59 +24,66 @@
[ext_resource path="res://assets/art/slash/slash-01.png" type="Texture" id=22] [ext_resource path="res://assets/art/slash/slash-01.png" type="Texture" id=22]
[ext_resource path="res://assets/art/slash/slash-04.png" type="Texture" id=23] [ext_resource path="res://assets/art/slash/slash-04.png" type="Texture" id=23]
[sub_resource type="AtlasTexture" id=1] [sub_resource type="RectangleShape2D" id=1]
flags = 4 extents = Vector2( 26.25, 28.75 )
atlas = ExtResource( 10 )
region = Rect2( 0, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=2] [sub_resource type="CapsuleShape2D" id=2]
flags = 4 radius = 8.82094
atlas = ExtResource( 10 ) height = 11.8581
region = Rect2( 45, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=3] [sub_resource type="AtlasTexture" id=3]
flags = 4 flags = 4
atlas = ExtResource( 10 ) atlas = ExtResource( 10 )
region = Rect2( 90, 0, 45, 47 ) region = Rect2( 0, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=4] [sub_resource type="AtlasTexture" id=4]
flags = 4 flags = 4
atlas = ExtResource( 10 ) atlas = ExtResource( 10 )
region = Rect2( 135, 0, 45, 47 ) region = Rect2( 45, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=5] [sub_resource type="AtlasTexture" id=5]
flags = 4 flags = 4
atlas = ExtResource( 8 ) atlas = ExtResource( 10 )
region = Rect2( 0, 0, 45, 47 ) region = Rect2( 90, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=6] [sub_resource type="AtlasTexture" id=6]
flags = 4 flags = 4
atlas = ExtResource( 8 ) atlas = ExtResource( 10 )
region = Rect2( 45, 0, 45, 47 ) region = Rect2( 135, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=7] [sub_resource type="AtlasTexture" id=7]
flags = 4 flags = 4
atlas = ExtResource( 8 ) atlas = ExtResource( 8 )
region = Rect2( 90, 0, 45, 47 ) region = Rect2( 0, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=8] [sub_resource type="AtlasTexture" id=8]
flags = 4 flags = 4
atlas = ExtResource( 6 ) atlas = ExtResource( 8 )
region = Rect2( 0, 0, 44, 47 ) region = Rect2( 45, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=9] [sub_resource type="AtlasTexture" id=9]
flags = 4 flags = 4
atlas = ExtResource( 6 ) atlas = ExtResource( 8 )
region = Rect2( 44, 0, 44, 47 ) region = Rect2( 90, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=10] [sub_resource type="AtlasTexture" id=10]
flags = 4 flags = 4
atlas = ExtResource( 6 ) atlas = ExtResource( 6 )
region = Rect2( 0, 0, 44, 47 )
[sub_resource type="AtlasTexture" id=11]
flags = 4
atlas = ExtResource( 6 )
region = Rect2( 44, 0, 44, 47 )
[sub_resource type="AtlasTexture" id=12]
flags = 4
atlas = ExtResource( 6 )
region = Rect2( 88, 0, 44, 47 ) region = Rect2( 88, 0, 44, 47 )
[sub_resource type="SpriteFrames" id=11] [sub_resource type="SpriteFrames" id=13]
animations = [ { animations = [ {
"frames": [ SubResource( 1 ), SubResource( 2 ), SubResource( 3 ), SubResource( 4 ) ], "frames": [ SubResource( 3 ), SubResource( 4 ), SubResource( 5 ), SubResource( 6 ) ],
"loop": true, "loop": true,
"name": "idle", "name": "idle",
"speed": 5.0 "speed": 5.0
@ -86,7 +93,7 @@ animations = [ {
"name": "slash", "name": "slash",
"speed": 5.0 "speed": 5.0
}, { }, {
"frames": [ SubResource( 5 ), SubResource( 6 ), SubResource( 7 ) ], "frames": [ SubResource( 7 ), SubResource( 8 ), SubResource( 9 ) ],
"loop": true, "loop": true,
"name": "forward", "name": "forward",
"speed": 5.0 "speed": 5.0
@ -96,7 +103,7 @@ animations = [ {
"name": "dash", "name": "dash",
"speed": 5.0 "speed": 5.0
}, { }, {
"frames": [ SubResource( 8 ), SubResource( 9 ), SubResource( 10 ) ], "frames": [ SubResource( 10 ), SubResource( 11 ), SubResource( 12 ) ],
"loop": true, "loop": true,
"name": "backward", "name": "backward",
"speed": 5.0 "speed": 5.0
@ -107,21 +114,31 @@ animations = [ {
"speed": 5.0 "speed": 5.0
} ] } ]
[sub_resource type="CapsuleShape2D" id=12] [node name="Player" type="KinematicBody2D"]
radius = 8.82094
height = 0.0
[node name="Player" type="Area2D"]
gravity = 0.0
script = ExtResource( 1 ) script = ExtResource( 1 )
__meta__ = {
"_edit_group_": true [node name="PlayerAttack" type="Node" parent="."]
} script = ExtResource( 5 )
[node name="Area2D" type="Area2D" parent="PlayerAttack"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="PlayerAttack/Area2D"]
position = Vector2( 10, 0 )
shape = SubResource( 1 )
[node name="AttackKey" type="Node" parent="."]
script = ExtResource( 7 )
[node name="DashKey" type="Node" parent="."]
script = ExtResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 2 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."] [node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 11 ) frames = SubResource( 13 )
animation = "idle" animation = "slash"
frame = 2 frame = 3
playing = true playing = true
[node name="PlayerNormal" type="Node" parent="."] [node name="PlayerNormal" type="Node" parent="."]
@ -132,16 +149,5 @@ script = ExtResource( 4 )
[node name="PlayerStanding" type="Node" parent="."] [node name="PlayerStanding" type="Node" parent="."]
script = ExtResource( 3 ) script = ExtResource( 3 )
[connection signal="body_entered" from="PlayerAttack/Area2D" to="PlayerAttack" method="_on_Area2D_body_entered"]
[node name="PlayerAttack" type="Node" parent="."]
script = ExtResource( 5 )
[node name="DashKey" type="Node" parent="."]
script = ExtResource( 2 )
[node name="AttackKey" type="Node" parent="."]
script = ExtResource( 7 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 12 )
[connection signal="animation_finished" from="AnimatedSprite" to="." method="_on_AnimatedSprite_animation_finished"] [connection signal="animation_finished" from="AnimatedSprite" to="." method="_on_AnimatedSprite_animation_finished"]

View File

@ -1,22 +1,39 @@
extends Node extends Node
class_name PlayerAttack class_name PlayerAttack
func run(actor, delta): const speed_scale = 4
const animation_name = "slash"
actor.getAnimatedSprite().animation = "slash" func input(actor):
actor.getAnimatedSprite().speed_scale = 4 actor.get_node("PlayerAttack/Area2D").show()
var velocity = actor.last_process_velocity actor.show()
pass
func process(actor):
actor.get_animated_sprite().animation = animation_name
actor.get_animated_sprite().speed_scale = speed_scale
actor.get_animated_sprite().flip_h = actor.velocity.x < 0
func physics_process(actor, delta):
var velocity = actor.velocity
velocity = velocity.normalized() * actor.speed velocity = velocity.normalized() * actor.speed
if velocity.length() > 0: if velocity.length() > 0:
actor.getAnimatedSprite().play() actor.get_animated_sprite().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
var collision = actor.move_and_collide(velocity * delta)
if collision:
collision.collider.on_collide(actor)
## BAD #### \/
if collision.collider.has_method("on_attack"):
collision.collider.on_attack()
func spriteFinished(actor): func sprite_finished(actor):
actor.get_node("PlayerAttack/Area2D").hide()
actor.state = actor.PlayerState.NORMAL actor.state = actor.PlayerState.NORMAL
actor.getAnimatedSprite().flip_h = false actor.get_animated_sprite().flip_h = false
actor.getAnimatedSprite().speed_scale = 1 actor.get_animated_sprite().speed_scale = 1
# WHY DOESN'T WORK?!?!?!?!?!?!?1?
func _on_Area2D_body_entered(body):
if body.has_method("on_attack"):
body.on_attack()

View File

@ -1,22 +1,35 @@
extends Node extends Node
class_name PlayerDash class_name PlayerDash
func run(actor, delta): var keymap
actor.getAnimatedSprite().animation = "dash" const speed_scale = 2.5
actor.getAnimatedSprite().speed_scale = 2.5 const animation_name = "dash"
var velocity = actor.last_process_velocity
func ready(actor):
var ak = AttackKey.new()
keymap = { KEY_Z: ak.register(actor) }
func input(actor):
actor.show()
pass
func process(actor):
actor.get_animated_sprite().animation = animation_name
actor.get_animated_sprite().speed_scale = speed_scale
actor.get_animated_sprite().flip_h = actor.velocity.x < 0
func physics_process(actor, delta):
var velocity = actor.velocity
velocity = velocity.normalized() * actor.speed velocity = velocity.normalized() * actor.speed
if velocity.length() > 0: if velocity.length() > 0:
actor.getAnimatedSprite().play() actor.get_animated_sprite().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
var collision = actor.move_and_collide(velocity * delta)
if collision:
collision.collider.on_collide(actor)
func spriteFinished(actor): func sprite_finished(actor):
actor.state = actor.PlayerState.STANDING actor.state = actor.PlayerState.STANDING
actor.getAnimatedSprite().flip_h = false actor.get_animated_sprite().flip_h = false
actor.getAnimatedSprite().speed_scale = 1 actor.get_animated_sprite().speed_scale = 1

View File

@ -1,14 +1,19 @@
extends Node extends Node
class_name PlayerNormal class_name PlayerNormal
func run(actor, delta): var keymap
func ready(actor):
var ak = AttackKey.new()
keymap = { KEY_Z: ak.register(actor) }
func input(actor):
var velocity = Vector2() # The player's movement vector. var velocity = Vector2() # The player's movement vector.
var keys = actor.key_map.keys() var keys = keymap.keys()
for i in keys: for i in keys:
if Input.is_key_pressed(i): if Input.is_key_pressed(i):
actor.key_map[i].trigger(actor) keymap[i].trigger(actor)
if Input.is_action_pressed("ui_right"): if Input.is_action_pressed("ui_right"):
velocity.x += 1 velocity.x += 1
@ -20,21 +25,22 @@ func run(actor, delta):
velocity.y -= 1 velocity.y -= 1
if velocity.length() > 0: if velocity.length() > 0:
velocity = velocity.normalized() * actor.speed velocity = velocity.normalized() * actor.speed
actor.getAnimatedSprite().play() actor.get_animated_sprite().play()
actor.position += velocity * delta actor.velocity = velocity
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: func process(actor):
actor.getAnimatedSprite().animation = "forward" if actor.velocity.x > 0 or actor.velocity.y != 0:
elif velocity.x < 0: actor.get_animated_sprite().animation = "forward"
actor.getAnimatedSprite().animation = "backward" elif actor.velocity.x < 0:
actor.get_animated_sprite().animation = "backward"
else: else:
actor.getAnimatedSprite().animation = "idle" actor.get_animated_sprite().animation = "idle"
actor.last_process_velocity = velocity func physics_process(actor, delta):
var collision = actor.move_and_collide(actor.velocity * delta)
if collision:
collision.collider.on_collide(actor)
func spriteFinished(actor): func sprite_finished(actor):
actor.show() actor.show()
pass

View File

@ -1,17 +1,23 @@
extends Node extends Node
class_name PlayerStanding class_name PlayerStanding
func run(actor, delta): const speed_scale = 1
const animation_name = "standing up"
actor.getAnimatedSprite().animation = "standing up" func input(actor):
actor.getAnimatedSprite().speed_scale = 1 actor.show()
var velocity = Vector2(0.0, 0.0) pass
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): func process(actor):
actor.get_animated_sprite().animation = animation_name
actor.get_animated_sprite().speed_scale = speed_scale
actor.get_animated_sprite().flip_h = actor.velocity.x < 0
func physics_process(actor, delta):
var collision = actor.move_and_collide(Vector2(0.0, 0.0) * delta)
if collision:
collision.collider.on_collide(actor)
func sprite_finished(actor):
actor.state = actor.PlayerState.NORMAL actor.state = actor.PlayerState.NORMAL
actor.getAnimatedSprite().flip_h = false actor.get_animated_sprite().flip_h = false

View File

@ -9,7 +9,7 @@
config_version=4 config_version=4
_global_script_classes=[ { _global_script_classes=[ {
"base": "Area2D", "base": "KinematicBody2D",
"class": "Actor", "class": "Actor",
"language": "GDScript", "language": "GDScript",
"path": "res://Actor.gd" "path": "res://Actor.gd"
@ -24,6 +24,11 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://DashKey.gd" "path": "res://DashKey.gd"
}, { }, {
"base": "Actor",
"class": "Enemy",
"language": "GDScript",
"path": "res://Enemy.gd"
}, {
"base": "Node", "base": "Node",
"class": "PlayerAttack", "class": "PlayerAttack",
"language": "GDScript", "language": "GDScript",
@ -48,6 +53,7 @@ _global_script_class_icons={
"Actor": "", "Actor": "",
"AttackKey": "", "AttackKey": "",
"DashKey": "", "DashKey": "",
"Enemy": "",
"PlayerAttack": "", "PlayerAttack": "",
"PlayerDash": "", "PlayerDash": "",
"PlayerNormal": "", "PlayerNormal": "",