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
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
func trigger(actor):
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
func _ready():
newGame()
new_game()
func newGame():
func new_game():
var player = preload("res://Player.tscn").instance()
player.name = "Player"
player.start($StartPosition.position)
@ -15,3 +15,7 @@ func newGame():
orb.scale = Vector2(0.4, 0.4)
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="."]
position = Vector2( 240, 450 )
[node name="Actor" type="Area2D" parent="."]
[node name="Actor" type="KinematicBody2D" parent="."]
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):
if area.name == "Player":
func on_collide(actor):
var dk = DashKey.new()
area.key_map[KEY_SHIFT] = dk.register(area)
var state = actor.PlayerState.NORMAL
actor.state_map[state].keymap[KEY_SHIFT] = dk.register(actor)
get_tree().queue_delete(self)

View File

@ -6,8 +6,7 @@
[sub_resource type="CircleShape2D" id=1]
radius = 33.9555
[node name="OrbItem" type="Area2D"]
gravity = 0.0
[node name="StaticBody2D" type="StaticBody2D"]
script = ExtResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
@ -26,4 +25,3 @@ text = "PICKUP TO DASH"
__meta__ = {
"_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 };
var screen_size # Size of the game window.
var last_process_velocity = Vector2()
var key_map
var velocity = Vector2()
var direction_vector = Vector2(1, 0) # to the right by default
func start(pos):
position = pos
state_map = {PlayerState.NORMAL: PlayerNormal.new(),
PlayerState.ATTACKING: PlayerAttack.new()}
var ak = AttackKey.new()
key_map = { KEY_Z: ak.register(self) }
state_map = {PlayerState.NORMAL: PlayerNormal.new()}
state_map[PlayerState.NORMAL].ready(self)
show()
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():
screen_size = get_viewport_rect().size
func _on_AnimatedSprite_animation_finished():
state_map[state].spriteFinished(self)
state_map[state].sprite_finished(self)
func getAnimatedSprite():
func get_animated_sprite():
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://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-04.png" type="Texture" id=23]
[sub_resource type="AtlasTexture" id=1]
flags = 4
atlas = ExtResource( 10 )
region = Rect2( 0, 0, 45, 47 )
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 26.25, 28.75 )
[sub_resource type="AtlasTexture" id=2]
flags = 4
atlas = ExtResource( 10 )
region = Rect2( 45, 0, 45, 47 )
[sub_resource type="CapsuleShape2D" id=2]
radius = 8.82094
height = 11.8581
[sub_resource type="AtlasTexture" id=3]
flags = 4
atlas = ExtResource( 10 )
region = Rect2( 90, 0, 45, 47 )
region = Rect2( 0, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=4]
flags = 4
atlas = ExtResource( 10 )
region = Rect2( 135, 0, 45, 47 )
region = Rect2( 45, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=5]
flags = 4
atlas = ExtResource( 8 )
region = Rect2( 0, 0, 45, 47 )
atlas = ExtResource( 10 )
region = Rect2( 90, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=6]
flags = 4
atlas = ExtResource( 8 )
region = Rect2( 45, 0, 45, 47 )
atlas = ExtResource( 10 )
region = Rect2( 135, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=7]
flags = 4
atlas = ExtResource( 8 )
region = Rect2( 90, 0, 45, 47 )
region = Rect2( 0, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=8]
flags = 4
atlas = ExtResource( 6 )
region = Rect2( 0, 0, 44, 47 )
atlas = ExtResource( 8 )
region = Rect2( 45, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=9]
flags = 4
atlas = ExtResource( 6 )
region = Rect2( 44, 0, 44, 47 )
atlas = ExtResource( 8 )
region = Rect2( 90, 0, 45, 47 )
[sub_resource type="AtlasTexture" id=10]
flags = 4
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 )
[sub_resource type="SpriteFrames" id=11]
[sub_resource type="SpriteFrames" id=13]
animations = [ {
"frames": [ SubResource( 1 ), SubResource( 2 ), SubResource( 3 ), SubResource( 4 ) ],
"frames": [ SubResource( 3 ), SubResource( 4 ), SubResource( 5 ), SubResource( 6 ) ],
"loop": true,
"name": "idle",
"speed": 5.0
@ -86,7 +93,7 @@ animations = [ {
"name": "slash",
"speed": 5.0
}, {
"frames": [ SubResource( 5 ), SubResource( 6 ), SubResource( 7 ) ],
"frames": [ SubResource( 7 ), SubResource( 8 ), SubResource( 9 ) ],
"loop": true,
"name": "forward",
"speed": 5.0
@ -96,7 +103,7 @@ animations = [ {
"name": "dash",
"speed": 5.0
}, {
"frames": [ SubResource( 8 ), SubResource( 9 ), SubResource( 10 ) ],
"frames": [ SubResource( 10 ), SubResource( 11 ), SubResource( 12 ) ],
"loop": true,
"name": "backward",
"speed": 5.0
@ -107,21 +114,31 @@ animations = [ {
"speed": 5.0
} ]
[sub_resource type="CapsuleShape2D" id=12]
radius = 8.82094
height = 0.0
[node name="Player" type="Area2D"]
gravity = 0.0
[node name="Player" type="KinematicBody2D"]
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="."]
frames = SubResource( 11 )
animation = "idle"
frame = 2
frames = SubResource( 13 )
animation = "slash"
frame = 3
playing = true
[node name="PlayerNormal" type="Node" parent="."]
@ -132,16 +149,5 @@ script = ExtResource( 4 )
[node name="PlayerStanding" type="Node" parent="."]
script = ExtResource( 3 )
[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="body_entered" from="PlayerAttack/Area2D" to="PlayerAttack" method="_on_Area2D_body_entered"]
[connection signal="animation_finished" from="AnimatedSprite" to="." method="_on_AnimatedSprite_animation_finished"]

View File

@ -1,22 +1,39 @@
extends Node
class_name PlayerAttack
func run(actor, delta):
const speed_scale = 4
const animation_name = "slash"
actor.getAnimatedSprite().animation = "slash"
actor.getAnimatedSprite().speed_scale = 4
var velocity = actor.last_process_velocity
func input(actor):
actor.get_node("PlayerAttack/Area2D").show()
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
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
actor.get_animated_sprite().play()
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.getAnimatedSprite().flip_h = false
actor.getAnimatedSprite().speed_scale = 1
actor.get_animated_sprite().flip_h = false
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
class_name PlayerDash
func run(actor, delta):
var keymap
actor.getAnimatedSprite().animation = "dash"
actor.getAnimatedSprite().speed_scale = 2.5
var velocity = actor.last_process_velocity
const speed_scale = 2.5
const animation_name = "dash"
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
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
actor.get_animated_sprite().play()
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.getAnimatedSprite().flip_h = false
actor.getAnimatedSprite().speed_scale = 1
actor.get_animated_sprite().flip_h = false
actor.get_animated_sprite().speed_scale = 1

View File

@ -1,14 +1,19 @@
extends Node
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 keys = actor.key_map.keys()
var keys = keymap.keys()
for i in keys:
if Input.is_key_pressed(i):
actor.key_map[i].trigger(actor)
keymap[i].trigger(actor)
if Input.is_action_pressed("ui_right"):
velocity.x += 1
@ -20,21 +25,22 @@ func run(actor, delta):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * actor.speed
actor.getAnimatedSprite().play()
actor.get_animated_sprite().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)
actor.velocity = velocity
if velocity.x > 0 or velocity.y != 0:
actor.getAnimatedSprite().animation = "forward"
elif velocity.x < 0:
actor.getAnimatedSprite().animation = "backward"
func process(actor):
if actor.velocity.x > 0 or actor.velocity.y != 0:
actor.get_animated_sprite().animation = "forward"
elif actor.velocity.x < 0:
actor.get_animated_sprite().animation = "backward"
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()
pass

View File

@ -1,17 +1,23 @@
extends Node
class_name PlayerStanding
func run(actor, delta):
const speed_scale = 1
const animation_name = "standing up"
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 input(actor):
actor.show()
pass
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.getAnimatedSprite().flip_h = false
actor.get_animated_sprite().flip_h = false

View File

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