From 7d875f9d8da0329af2aadef350b074748f000342 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 15 Jul 2021 01:13:19 +0200 Subject: [PATCH] Implemented movement and standing animation support for the new spritesheet based character. Replaced the old character with it. --- game/characters/CharacterSprite.gd | 64 ++++++++++++++++++++++++++-- game/characters/SheetCharacter.gd | 16 ++++++- game/characters/SheetCharacter.tscn | 3 +- game/modules/species/Human/Body.tscn | 52 ++-------------------- game/player/Body.gd | 9 +++- 5 files changed, 87 insertions(+), 57 deletions(-) diff --git a/game/characters/CharacterSprite.gd b/game/characters/CharacterSprite.gd index 35601bd..8565dfc 100644 --- a/game/characters/CharacterSprite.gd +++ b/game/characters/CharacterSprite.gd @@ -1,5 +1,19 @@ extends Sprite +enum Facing { + FACING_RIGHT = 0, + FACING_LEFT = 1, + FACING_FRONT = 2, + FACING_BACK = 3 +} + +enum Animations { + ANIMATION_RUN = 0, + ANIMATION_WALK = 1, + ANIMATION_CAST = 2, + ANIMATION_STAND = 3 +} + export(bool) var enabled : bool = true export(float) var timer : float = 0.13 @@ -16,9 +30,13 @@ var y = 0 var _timer : float = 0 var _frame : int = 0 +var _facing : int = Facing.FACING_RIGHT +var _current_animation : int = Animations.ANIMATION_STAND + # Called when the node enters the scene tree for the first time. func _ready(): - set_frame(_min_frame) + #set_frame(_min_frame) + update_animset() #_sprite.set_position(Vector2(x, y)) func set_movement_vector(vector): @@ -35,6 +53,8 @@ func set_movement_vector(vector): set_animset(num_slice) + + func set_animset(animest_id): if (animest_id >= _animset_count): _animset = _animset_count - 1 @@ -48,6 +68,9 @@ func set_animset(animest_id): #_min_frame = _animset * _animset_size #_max_frame = _animset * _animset_size + _animset_size +func update_animset(): + _animset = _current_animation * 4 + (_facing) + func _process(delta): #set_animset(_animset) @@ -55,10 +78,10 @@ func _process(delta): set_process(false) return - timer += delta + _timer += delta - if (timer > 0.05): - timer -= 0.05 + if (_timer > timer): + _timer -= timer _frame += 1 if _frame > _animset_size - 1: @@ -66,6 +89,39 @@ func _process(delta): set_frame(_frame + (_animset * _animset_size)) +func set_to_move(): + if _current_animation != Animations.ANIMATION_RUN: + _current_animation = Animations.ANIMATION_RUN + _frame = 0 + +func set_facing(input_direction : Vector2) -> void: + var front : bool = abs(input_direction.dot(Vector2(0, 1))) > 0.9 + + if front: + if input_direction.y > 0: + _facing = Facing.FACING_FRONT + else: + _facing = Facing.FACING_BACK + else: + if input_direction.x > 0.01: + _facing = Facing.FACING_RIGHT + elif input_direction.x < -0.01: + _facing = Facing.FACING_LEFT + + update_animset() + +func set_to_stand(): + if _current_animation != Animations.ANIMATION_STAND: + _current_animation = Animations.ANIMATION_STAND + _frame = 0 + update_animset() + +func set_to_cast(): + if _current_animation != Animations.ANIMATION_CAST: + _current_animation = Animations.ANIMATION_CAST + _frame = 0 + update_animset() + # Called every frame. 'delta' is the elapsed time since the previous frame. func a_process(delta): #x += int(delta * 300) diff --git a/game/characters/SheetCharacter.gd b/game/characters/SheetCharacter.gd index b542001..be0576c 100644 --- a/game/characters/SheetCharacter.gd +++ b/game/characters/SheetCharacter.gd @@ -3,6 +3,7 @@ extends CharacterSkeleton2D export(NodePath) var sprite_path : NodePath var _sprite : Sprite +var _standing = true func _ready(): _sprite = get_node(sprite_path) @@ -10,4 +11,17 @@ func _ready(): func get_sprite() -> Sprite: return _sprite - +func move_dir(input_dir): + if _standing: + _standing = false + _sprite.set_to_move() + + _sprite.set_facing(input_dir) + +func stand(): + if _standing: + return + + _standing = true + + _sprite.set_to_stand() diff --git a/game/characters/SheetCharacter.tscn b/game/characters/SheetCharacter.tscn index c409cc1..535982d 100644 --- a/game/characters/SheetCharacter.tscn +++ b/game/characters/SheetCharacter.tscn @@ -6,6 +6,7 @@ [node name="SheetCharacter" type="CharacterSkeleton2D"] script = ExtResource( 3 ) +sprite_path = NodePath("CharacterSprite") [node name="CharacterSprite" type="Sprite" parent="."] position = Vector2( 0, -11.906 ) @@ -13,4 +14,4 @@ texture = ExtResource( 1 ) hframes = 16 vframes = 16 script = ExtResource( 2 ) -timer = 0.05 +timer = 0.07 diff --git a/game/modules/species/Human/Body.tscn b/game/modules/species/Human/Body.tscn index ef1ca1c..9fccd35 100644 --- a/game/modules/species/Human/Body.tscn +++ b/game/modules/species/Human/Body.tscn @@ -1,63 +1,17 @@ -[gd_scene load_steps=11 format=2] +[gd_scene load_steps=4 format=2] -[ext_resource path="res://characters/Character.tscn" type="PackedScene" id=1] -[ext_resource path="res://scripts/item_visuals/CharacterAtlasEntry2D.gd" type="Script" id=2] -[ext_resource path="res://scripts/item_visuals/CharacterAtlas2D.gd" type="Script" id=3] -[ext_resource path="res://characters/char_t1_outline_split.png" type="Texture" id=5] [ext_resource path="res://player/Body.gd" type="Script" id=6] [ext_resource path="res://characters/SheetCharacter.tscn" type="PackedScene" id=7] -[sub_resource type="CharacterAtlasEntry" id=1] -script = ExtResource( 2 ) -front_rect = Rect2( 101, 15, 9, 12 ) -back_rect = Rect2( 74, 18, 9, 12 ) -right_rect = Rect2( 8, 13, 9, 12 ) -left_rect = Rect2( 8, 13, 9, 12 ) - -[sub_resource type="CharacterAtlasEntry" id=2] -script = ExtResource( 2 ) -front_rect = Rect2( 102, 2, 7, 8 ) -back_rect = Rect2( 75, 5, 7, 8 ) -right_rect = Rect2( 9, 0, 7, 8 ) -left_rect = Rect2( 9, 0, 7, 8 ) - -[sub_resource type="CharacterAtlas" id=3] -script = ExtResource( 3 ) -comments = "HUMANOID_BONE_HIP = 0, -HUMANOID_BONE_TORSO = 1, -HUMANOID_BONE_LEG_R = 2, -HUMANOID_BONE_FOOT_R = 3, -HUMANOID_BONE_TOE_R = 4, -HUMANOID_BONE_LEG_L = 5, -HUMANOID_BONE_FOOT_L = 6, -HUMANOID_BONE_TOE_L = 7, -HUMANOID_BONE_ARM_R = 8, -#HUMANOID_BONE_SHOULDER_GUARD_R = 0, -HUMANOID_BONE_HAND_R = 9, -HUMANOID_BONE_FINGERS_R = 10, -HUMANOID_BONE_ARM_L = 11, -#HUMANOID_BONE_SHOULDER_GUARD_L = 0, -HUMANOID_BONE_HAND_L = 12, -HUMANOID_BONE_FINGERS_L = 13, -HUMANOID_BONE_HEAD = 14, -HUMANOID_BONE_HEAD_TOP = 15, -HUMANOID_BONES_MAX = 16," -texture = ExtResource( 5 ) -slots = [ null, SubResource( 1 ), null, null, null, null, null, null, null, null, null, null, null, null, SubResource( 2 ), null ] - [sub_resource type="CircleShape2D" id=4] radius = 8.0 [node name="Body" type="KinematicBody2D"] script = ExtResource( 6 ) +model_path = NodePath("") +character_skeleton_path = NodePath("SheetCharacter") [node name="SheetCharacter" parent="." instance=ExtResource( 7 )] -visible = false - -[node name="Character" parent="." instance=ExtResource( 1 )] -front_node_path = NodePath("../../Body/Character/CharacterFrontModel") -side_node_path = NodePath("../../Body/Character/CharacterSideModel") -character_atlas = SubResource( 3 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource( 4 ) diff --git a/game/player/Body.gd b/game/player/Body.gd index b6996f9..be7415c 100644 --- a/game/player/Body.gd +++ b/game/player/Body.gd @@ -184,9 +184,14 @@ func process_input(delta: float) -> void: if input_length > 0.1: #handle_graphic_facing(abs(dir.dot(Vector2(0, 1))) > 0.9) - character_skeleton.update_facing(input_dir) + character_skeleton.move_dir(input_dir) + else: + character_skeleton.stand() - character_skeleton.get_animation_tree().set("parameters/walking/blend_amount", input_dir.length()) + #character_skeleton.get_animation_tree().set("parameters/walking/blend_amount", input_dir.length()) + #character_skeleton.set_ + + #character_skeleton.update_facing(input_dir) func process_movement(delta : float) -> void: