More modular game Actor setup.

This commit is contained in:
Nitsud Yarg 2024-05-30 23:43:32 -07:00
parent c4df444848
commit 66a0de16bb
10 changed files with 83 additions and 72 deletions

View File

@ -19,10 +19,10 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/playerC/resources/FrameSounds.gd"
}, {
"base": "Reference",
"class": "Movement",
"base": "Node",
"class": "MovementComponent",
"language": "GDScript",
"path": "res://src/playerC/player_move_component.gd"
"path": "res://src/movement_component.gd"
}, {
"base": "Node",
"class": "State",
@ -52,7 +52,7 @@ _global_script_classes=[ {
_global_script_class_icons={
"Actor": "",
"FrameSoundEffects": "",
"Movement": "",
"MovementComponent": "",
"State": "",
"StateAnimatedActor": "",
"StateMachine": "",

View File

@ -1,17 +1,39 @@
class_name Actor
extends KinematicBody2D
var velocity = Vector2(0,0)
var direction = Vector2(-1,0)
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
export var sprite_facing_right: bool = true
export(Array, Resource) var SoundEffects
onready var movement_animations: AnimatedSprite = $AnimatedSprite
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
onready var sound_effect_player = $SE_Player
onready var movement_state_machine: StateMachineAnimatedActor = $Controllers/state_machine
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
onready var movement_component = $Controllers/move_component
var sound_effects_dict: Dictionary
##TODO:
# Can't seem to implement the ready and process node functions
# without causing a double run somehow. Not sure why.
# not a huge deal because this is mostly to help with
# interfaces for the state machines.
var last_sound_frame_animation: String = ''
var last_sound_frame: int = -1
func play_sound_frame(animation: String, frame: int ):
##DEBUG:
#if animation == 'run' and frame == 6:
# print("Do the thign! (", animation + str(frame), ")", " (", last_sound_frame_animation + str(last_sound_frame), ")")
if animation != last_sound_frame_animation or frame != last_sound_frame:
last_sound_frame = frame
last_sound_frame_animation = animation
var sound_index = animation + str(frame)
if sound_effects_dict.has(sound_index):
sound_effect_player.stream = sound_effects_dict[sound_index]
sound_effect_player.play()
print("Playing SE: ", sound_index)

18
src/movement_component.gd Normal file
View File

@ -0,0 +1,18 @@
class_name MovementComponent
extends Node
var desired_movement_vector: Vector2
# Return the desired direction of movement for the character
# in the range [-1, 1], where positive values indicate a desire
# to move to the right and negative values to the left.
func get_movement_direction() -> float:
return 0.0
# Return a boolean indicating if the character wants to jump
func wants_jump() -> bool:
return false
# Return a boolean indicating if the character wants to jump
func wants_shoot() -> bool:
return false

View File

@ -1,32 +1,17 @@
extends KinematicBody2D
var velocity = Vector2(0,0)
var direction = Vector2(-1,0)
extends Actor
export var player_number: int = 1
export var sprite_facing_right: bool = true
onready var movement_animations: AnimatedSprite = $AnimatedSprite
export(Array, Resource) var SoundEffects
onready var movement_state_machine: Node = $Controllers/state_machine
onready var player_move_component = $Controllers/move_component
onready var player_hurtbox = $Hurtbox_Component/CollisionShape2D
onready var gun = $Gun
onready var sound_effect_player = $SE_Player
#var sound_effects = preload("res://src/playerC/resources/sound_effects.tres")
var sound_effects_dict: Dictionary
func _ready() -> void:
movement_state_machine.init_animated_actor(self, movement_animations, player_move_component)
player_move_component.player_number = player_number
movement_state_machine.init_animated_actor(self, movement_animations, movement_component)
movement_component.player_number = player_number
# Experimenting with sound based resources I did this and it worked
#sound_effect_player.stream = sound_effects.sounds["pew"]
@ -94,18 +79,4 @@ func shoot_projectile():
#func play_sound():
# sound_effects.stream = "res://assets/Sounds/crappy pew.wav"
var last_sound_frame_animation: String = ''
var last_sound_frame: int = -1
func play_sound_frame(animation: String, frame: int ):
##DEBUG:
#if animation == 'run' and frame == 6:
# print("Do the thign! (", animation + str(frame), ")", " (", last_sound_frame_animation + str(last_sound_frame), ")")
if animation != last_sound_frame_animation or frame != last_sound_frame:
last_sound_frame = frame
last_sound_frame_animation = animation
var sound_index = animation + str(frame)
if sound_effects_dict.has(sound_index):
sound_effect_player.stream = sound_effects_dict[sound_index]
sound_effect_player.play()
print("Playing SE: ", sound_index)

View File

@ -13,7 +13,7 @@
[ext_resource path="res://src/playerC/resources/FrameSounds.gd" type="Script" id=11]
[ext_resource path="res://src/components/Hurtbox_Component.tscn" type="PackedScene" id=12]
[ext_resource path="res://src/state_modifier.gd" type="Script" id=13]
[ext_resource path="res://src/state_machine_animated_actor.gd" type="Script" id=14]
[ext_resource path="res://src/playerC/sm.gd" type="Script" id=14]
[sub_resource type="Resource" id=148]
script = ExtResource( 11 )

View File

@ -1,12 +1,10 @@
class_name Movement
extends Node
extends MovementComponent
export var flipped_sprite: bool = true
## Deprecated
#export var flipped_sprite: bool = true
export var player_number: int = 1
var desired_movement_vector: Vector2
# Return the desired direction of movement for the character
# in the range [-1, 1], where positive values indicate a desire
# to move to the right and negative values to the left.

2
src/playerC/sm.gd Normal file
View File

@ -0,0 +1,2 @@
extends StateMachineAnimatedActor

View File

@ -29,13 +29,13 @@ func process_physics(delta: float) -> State:
elif parent.velocity.x < 0:
parent.direction.x = -1
if move_component.flipped_sprite == false:
#parent.scale.x = parent.direction.x * -1
parent.transform.x.x = parent.direction.x * -1 # -1
else:
#parent.scale.x = parent.direction.x
parent.transform.x.x = parent.direction.x # 1
parent.transform.x.x = parent.direction.x
# if move_component.flipped_sprite == false:
# #parent.scale.x = parent.direction.x * -1
# parent.transform.x.x = parent.direction.x * -1 # -1
# else:
# #parent.scale.x = parent.direction.x
# parent.transform.x.x = parent.direction.x # 1
if parent.is_on_floor():
if parent.velocity.x == 0:

View File

@ -33,13 +33,13 @@ func process_physics(delta: float) -> State:
parent.direction.x = 1
elif parent.velocity.x < 0:
parent.direction.x = -1
if move_component.flipped_sprite == false:
#parent.scale.x = parent.direction.x * -1
parent.transform.x.x = parent.direction.x * -1 # -1
else:
#parent.scale.x = parent.direction.x
parent.transform.x.x = parent.direction.x # 1
parent.transform.x.x = parent.direction.x
# if move_component.flipped_sprite == false:
# #parent.scale.x = parent.direction.x * -1
# parent.transform.x.x = parent.direction.x * -1 # -1
# else:
# #parent.scale.x = parent.direction.x
# parent.transform.x.x = parent.direction.x # 1
if parent.is_on_floor():
if parent.velocity.x == 0:

View File

@ -51,19 +51,19 @@ func process_physics(delta: float) -> State:
elif parent.velocity.x < 0:
parent.direction.x = -1
parent.transform.x.x = parent.direction.x
# if move_component.flipped_sprite == true:
# animations.flip_h = parent.direction.x > 0
# else:
# animations.flip_h = parent.direction.x < 0
# Attempting to flip based on scale instead of sprite flip so that
# all children nodes get flipped as well:
if move_component.flipped_sprite == false:
#parent.scale.x = parent.direction.x * -1
parent.transform.x.x = parent.direction.x * -1 # -1
else:
#parent.scale.x = parent.direction.x
parent.transform.x.x = parent.direction.x # 1
# if move_component.flipped_sprite == false:
# #parent.scale.x = parent.direction.x * -1
# parent.transform.x.x = parent.direction.x * -1 # -1
# else:
# #parent.scale.x = parent.direction.x
# parent.transform.x.x = parent.direction.x # 1