More modular game Actor setup.
This commit is contained in:
parent
c4df444848
commit
66a0de16bb
|
|
@ -19,10 +19,10 @@ _global_script_classes=[ {
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://src/playerC/resources/FrameSounds.gd"
|
"path": "res://src/playerC/resources/FrameSounds.gd"
|
||||||
}, {
|
}, {
|
||||||
"base": "Reference",
|
"base": "Node",
|
||||||
"class": "Movement",
|
"class": "MovementComponent",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://src/playerC/player_move_component.gd"
|
"path": "res://src/movement_component.gd"
|
||||||
}, {
|
}, {
|
||||||
"base": "Node",
|
"base": "Node",
|
||||||
"class": "State",
|
"class": "State",
|
||||||
|
|
@ -52,7 +52,7 @@ _global_script_classes=[ {
|
||||||
_global_script_class_icons={
|
_global_script_class_icons={
|
||||||
"Actor": "",
|
"Actor": "",
|
||||||
"FrameSoundEffects": "",
|
"FrameSoundEffects": "",
|
||||||
"Movement": "",
|
"MovementComponent": "",
|
||||||
"State": "",
|
"State": "",
|
||||||
"StateAnimatedActor": "",
|
"StateAnimatedActor": "",
|
||||||
"StateMachine": "",
|
"StateMachine": "",
|
||||||
|
|
|
||||||
40
src/actor.gd
40
src/actor.gd
|
|
@ -1,17 +1,39 @@
|
||||||
class_name Actor
|
class_name Actor
|
||||||
extends KinematicBody2D
|
extends KinematicBody2D
|
||||||
|
|
||||||
|
var velocity = Vector2(0,0)
|
||||||
|
var direction = Vector2(-1,0)
|
||||||
|
|
||||||
# Declare member variables here. Examples:
|
export var sprite_facing_right: bool = true
|
||||||
# var a = 2
|
export(Array, Resource) var SoundEffects
|
||||||
# var b = "text"
|
|
||||||
|
|
||||||
|
onready var movement_animations: AnimatedSprite = $AnimatedSprite
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
onready var sound_effect_player = $SE_Player
|
||||||
func _ready():
|
|
||||||
pass # Replace with function body.
|
|
||||||
|
|
||||||
|
onready var movement_state_machine: StateMachineAnimatedActor = $Controllers/state_machine
|
||||||
|
|
||||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
onready var movement_component = $Controllers/move_component
|
||||||
#func _process(delta):
|
|
||||||
# pass
|
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
18
src/movement_component.gd
Normal 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
|
||||||
|
|
@ -1,32 +1,17 @@
|
||||||
extends KinematicBody2D
|
extends Actor
|
||||||
|
|
||||||
var velocity = Vector2(0,0)
|
|
||||||
var direction = Vector2(-1,0)
|
|
||||||
|
|
||||||
export var player_number: int = 1
|
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 player_hurtbox = $Hurtbox_Component/CollisionShape2D
|
||||||
|
|
||||||
onready var gun = $Gun
|
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 = preload("res://src/playerC/resources/sound_effects.tres")
|
||||||
var sound_effects_dict: Dictionary
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
movement_state_machine.init_animated_actor(self, movement_animations, player_move_component)
|
movement_state_machine.init_animated_actor(self, movement_animations, movement_component)
|
||||||
player_move_component.player_number = player_number
|
movement_component.player_number = player_number
|
||||||
|
|
||||||
# Experimenting with sound based resources I did this and it worked
|
# Experimenting with sound based resources I did this and it worked
|
||||||
#sound_effect_player.stream = sound_effects.sounds["pew"]
|
#sound_effect_player.stream = sound_effects.sounds["pew"]
|
||||||
|
|
@ -94,18 +79,4 @@ func shoot_projectile():
|
||||||
#func play_sound():
|
#func play_sound():
|
||||||
# sound_effects.stream = "res://assets/Sounds/crappy pew.wav"
|
# 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)
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
[ext_resource path="res://src/playerC/resources/FrameSounds.gd" type="Script" id=11]
|
[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/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_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]
|
[sub_resource type="Resource" id=148]
|
||||||
script = ExtResource( 11 )
|
script = ExtResource( 11 )
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,10 @@
|
||||||
class_name Movement
|
extends MovementComponent
|
||||||
extends Node
|
|
||||||
|
|
||||||
export var flipped_sprite: bool = true
|
## Deprecated
|
||||||
|
#export var flipped_sprite: bool = true
|
||||||
|
|
||||||
export var player_number: int = 1
|
export var player_number: int = 1
|
||||||
|
|
||||||
var desired_movement_vector: Vector2
|
|
||||||
|
|
||||||
# Return the desired direction of movement for the character
|
# Return the desired direction of movement for the character
|
||||||
# in the range [-1, 1], where positive values indicate a desire
|
# in the range [-1, 1], where positive values indicate a desire
|
||||||
# to move to the right and negative values to the left.
|
# to move to the right and negative values to the left.
|
||||||
|
|
|
||||||
2
src/playerC/sm.gd
Normal file
2
src/playerC/sm.gd
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
extends StateMachineAnimatedActor
|
||||||
|
|
||||||
|
|
@ -29,13 +29,13 @@ func process_physics(delta: float) -> State:
|
||||||
elif parent.velocity.x < 0:
|
elif parent.velocity.x < 0:
|
||||||
parent.direction.x = -1
|
parent.direction.x = -1
|
||||||
|
|
||||||
|
parent.transform.x.x = parent.direction.x
|
||||||
if move_component.flipped_sprite == false:
|
# if move_component.flipped_sprite == false:
|
||||||
#parent.scale.x = parent.direction.x * -1
|
# #parent.scale.x = parent.direction.x * -1
|
||||||
parent.transform.x.x = parent.direction.x * -1 # -1
|
# parent.transform.x.x = parent.direction.x * -1 # -1
|
||||||
else:
|
# else:
|
||||||
#parent.scale.x = parent.direction.x
|
# #parent.scale.x = parent.direction.x
|
||||||
parent.transform.x.x = parent.direction.x # 1
|
# parent.transform.x.x = parent.direction.x # 1
|
||||||
|
|
||||||
if parent.is_on_floor():
|
if parent.is_on_floor():
|
||||||
if parent.velocity.x == 0:
|
if parent.velocity.x == 0:
|
||||||
|
|
|
||||||
|
|
@ -33,13 +33,13 @@ func process_physics(delta: float) -> State:
|
||||||
parent.direction.x = 1
|
parent.direction.x = 1
|
||||||
elif parent.velocity.x < 0:
|
elif parent.velocity.x < 0:
|
||||||
parent.direction.x = -1
|
parent.direction.x = -1
|
||||||
|
parent.transform.x.x = parent.direction.x
|
||||||
if move_component.flipped_sprite == false:
|
# if move_component.flipped_sprite == false:
|
||||||
#parent.scale.x = parent.direction.x * -1
|
# #parent.scale.x = parent.direction.x * -1
|
||||||
parent.transform.x.x = parent.direction.x * -1 # -1
|
# parent.transform.x.x = parent.direction.x * -1 # -1
|
||||||
else:
|
# else:
|
||||||
#parent.scale.x = parent.direction.x
|
# #parent.scale.x = parent.direction.x
|
||||||
parent.transform.x.x = parent.direction.x # 1
|
# parent.transform.x.x = parent.direction.x # 1
|
||||||
|
|
||||||
if parent.is_on_floor():
|
if parent.is_on_floor():
|
||||||
if parent.velocity.x == 0:
|
if parent.velocity.x == 0:
|
||||||
|
|
|
||||||
|
|
@ -51,19 +51,19 @@ func process_physics(delta: float) -> State:
|
||||||
elif parent.velocity.x < 0:
|
elif parent.velocity.x < 0:
|
||||||
parent.direction.x = -1
|
parent.direction.x = -1
|
||||||
|
|
||||||
|
parent.transform.x.x = parent.direction.x
|
||||||
# if move_component.flipped_sprite == true:
|
# if move_component.flipped_sprite == true:
|
||||||
# animations.flip_h = parent.direction.x > 0
|
# animations.flip_h = parent.direction.x > 0
|
||||||
# else:
|
# else:
|
||||||
# animations.flip_h = parent.direction.x < 0
|
# animations.flip_h = parent.direction.x < 0
|
||||||
# Attempting to flip based on scale instead of sprite flip so that
|
# Attempting to flip based on scale instead of sprite flip so that
|
||||||
# all children nodes get flipped as well:
|
# all children nodes get flipped as well:
|
||||||
if move_component.flipped_sprite == false:
|
# if move_component.flipped_sprite == false:
|
||||||
#parent.scale.x = parent.direction.x * -1
|
# #parent.scale.x = parent.direction.x * -1
|
||||||
parent.transform.x.x = parent.direction.x * -1 # -1
|
# parent.transform.x.x = parent.direction.x * -1 # -1
|
||||||
else:
|
# else:
|
||||||
#parent.scale.x = parent.direction.x
|
# #parent.scale.x = parent.direction.x
|
||||||
parent.transform.x.x = parent.direction.x # 1
|
# parent.transform.x.x = parent.direction.x # 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user