Moved actor direction into movement component. Velocity is going to be tough.

This commit is contained in:
Nitsud Yarg 2024-06-02 10:25:53 -07:00
parent 2a54a8f8fd
commit 50c1c9db51
13 changed files with 65 additions and 53 deletions

File diff suppressed because one or more lines are too long

View File

@ -2,11 +2,12 @@ class_name Actor
extends KinematicBody2D extends KinematicBody2D
var velocity = Vector2(0,0) var velocity = Vector2(0,0)
var direction = Vector2(-1,0) #var direction = Vector2(-1,0)
export(String, "Player", "Enemy", "NPC") var actor_type export(String, "Player", "Enemy", "NPC") var actor_type
export var sprite_facing_right: bool = true #export var sprite_facing_right: bool = true
export(Array, Resource) var SoundEffects export(Array, Resource) var SoundEffects
export var debug_actor: bool = false
onready var movement_animations: AnimatedSprite = $AnimatedSprite onready var movement_animations: AnimatedSprite = $AnimatedSprite
@ -44,6 +45,7 @@ func _unhandled_input(event: InputEvent) -> void:
movement_state_machine.process_input(event) movement_state_machine.process_input(event)
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
movement_component.process_physics(delta)
movement_state_machine.process_physics(delta) movement_state_machine.process_physics(delta)
func _process(delta: float) -> void: func _process(delta: float) -> void:
@ -67,4 +69,4 @@ func play_sound_frame(animation: String, frame: int ):
if sound_effects_dict.has(sound_index): if sound_effects_dict.has(sound_index):
sound_effect_player.stream = sound_effects_dict[sound_index] sound_effect_player.stream = sound_effects_dict[sound_index]
sound_effect_player.play() sound_effect_player.play()
print("Playing SE: ", sound_index) print(self.name, " Playing SE: ", sound_index)

View File

@ -117,7 +117,7 @@ actor_type = "Enemy"
position = Vector2( 11, -7 ) position = Vector2( 11, -7 )
frames = SubResource( 20 ) frames = SubResource( 20 )
animation = "idle" animation = "idle"
frame = 0 frame = 1
__meta__ = { __meta__ = {
"_aseprite_wizard_config_": { "_aseprite_wizard_config_": {
"layer": "", "layer": "",
@ -156,10 +156,18 @@ animation_sequence = [ "attack" ]
[node name="move" parent="movement_state_machine" index="3"] [node name="move" parent="movement_state_machine" index="3"]
animation_sequence = [ "walk" ] animation_sequence = [ "walk" ]
[node name="PlayerDetection" type="RayCast2D" parent="." index="6"] [node name="EdgeDetection" type="RayCast2D" parent="." index="6"]
modulate = Color( 0.054902, 1, 0, 1 )
show_behind_parent = true
position = Vector2( 15, 3 )
enabled = true
cast_to = Vector2( 0, 17 )
collide_with_areas = true
[node name="PlayerDetection" type="RayCast2D" parent="." index="7"]
cast_to = Vector2( 21, 0 ) cast_to = Vector2( 21, 0 )
[node name="Hitbox_Component" type="Area2D" parent="." index="7"] [node name="Hitbox_Component" type="Area2D" parent="." index="8"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Hitbox_Component" index="0"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Hitbox_Component" index="0"]
modulate = Color( 1, 0.596078, 0.121569, 1 ) modulate = Color( 1, 0.596078, 0.121569, 1 )

View File

@ -1,11 +1,19 @@
extends MovementComponent extends MovementComponent
onready var idle_timeout = $IdleTimeout onready var idle_timeout = $IdleTimeout
onready var edge_detector:RayCast2D = $"../EdgeDetection"
var flip_flop = false var flip_flop = false
func process_physics(delta):
if current_movement_state == "move" and edge_detector.is_colliding() == false:
#print(owner.name, "uh oh gonna fall!")
stop()
return
func process(delta): func process(delta):
if current_movement_state == "idle" and idle_timeout.time_left == 0: if current_movement_state == "idle" and idle_timeout.time_left == 0:
if debug_component:
print ("Time to move!") print ("Time to move!")
if flip_flop: if flip_flop:
flip_flop = false flip_flop = false
@ -16,6 +24,7 @@ func process(delta):
go_left() go_left()
idle_timeout.start() idle_timeout.start()
if current_movement_state == "move" and idle_timeout.time_left == 0: if current_movement_state == "move" and idle_timeout.time_left == 0:
if debug_component:
print("Time to stop.") print("Time to stop.")
idle_timeout.start() idle_timeout.start()
stop() stop()

View File

@ -1,6 +1,8 @@
class_name MovementComponent class_name MovementComponent
extends Node extends Node
export var debug_component: bool = false
onready var desired_movement_vector: Vector2 = Vector2(0,0) onready var desired_movement_vector: Vector2 = Vector2(0,0)
var current_movement_state:String var current_movement_state:String
@ -12,6 +14,9 @@ const DOWN = 1.0
const LEFT = -1.0 const LEFT = -1.0
const RIGHT = 1.0 const RIGHT = 1.0
func process_physics(delta):
pass
func process(delta): func process(delta):
pass pass

View File

@ -28,7 +28,7 @@ func set_hurtbox(enabled: bool):
func shoot_projectile(): func shoot_projectile():
var is_shooting = false var is_shooting = false
is_shooting = gun.shoot(direction.x) is_shooting = gun.shoot(movement_component.get_movement_direction())
#func play_sound(): #func play_sound():
# sound_effects.stream = "res://assets/Sounds/crappy pew.wav" # sound_effects.stream = "res://assets/Sounds/crappy pew.wav"

View File

@ -24,12 +24,8 @@ func process_physics(delta: float) -> State:
# if parent.velocity.x != 0.0: # if parent.velocity.x != 0.0:
# parent.direction.x = parent.velocity.x # parent.direction.x = parent.velocity.x
if parent.velocity.x > 0: if move_component.get_movement_direction() != 0.0:
parent.direction.x = 1 parent.transform.x.x = move_component.get_movement_direction()
elif parent.velocity.x < 0:
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

View File

@ -37,10 +37,11 @@ func process_input(_event: InputEvent) -> State:
return null return null
func process_physics(delta: float) -> State: func process_physics(delta: float) -> State:
if debug_state:
debugTimeTracker += delta debugTimeTracker += delta
if debugTimeTracker > 2: if debugTimeTracker > 2:
debugTimeTracker = 0.0 debugTimeTracker = 0.0
print("DEBUG TRANSFORM:", parent.transform) print(owner.name, " DEBUG TRANSFORM:", parent.transform)
parent.velocity.y += gravity * delta parent.velocity.y += gravity * delta
#parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1)) #parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1))

View File

@ -29,11 +29,9 @@ func process_physics(delta: float) -> State:
parent.velocity.x = move_component.desired_movement_vector.x * move_speed parent.velocity.x = move_component.desired_movement_vector.x * move_speed
parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1)) parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1))
if parent.velocity.x > 0: if move_component.get_movement_direction() != 0.0:
parent.direction.x = 1 parent.transform.x.x = move_component.get_movement_direction()
elif parent.velocity.x < 0:
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

View File

@ -24,6 +24,7 @@ func process_input(_event: InputEvent) -> State:
func process_physics(delta: float) -> State: func process_physics(delta: float) -> State:
# if move_component.wants_jump() and parent.is_on_floor(): # if move_component.wants_jump() and parent.is_on_floor():
# return jump_state # return jump_state
if debug_state:
debugTimeTracker += delta debugTimeTracker += delta
if debugTimeTracker > 2: if debugTimeTracker > 2:
debugTimeTracker = 0.0 debugTimeTracker = 0.0
@ -45,13 +46,10 @@ func process_physics(delta: float) -> State:
if parent.velocity.x == 0.0: if parent.velocity.x == 0.0:
return idle_state return idle_state
else:
if parent.velocity.x > 0:
parent.direction.x = 1
elif parent.velocity.x < 0:
parent.direction.x = -1
parent.transform.x.x = parent.direction.x if move_component.get_movement_direction() != 0.0:
parent.transform.x.x = move_component.get_movement_direction()
# 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:

View File

@ -10,12 +10,8 @@ func process_physics(delta: float) -> State:
parent.velocity.x = move_component.desired_movement_vector.x * move_speed parent.velocity.x = move_component.desired_movement_vector.x * move_speed
parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1)) parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1))
if parent.velocity.x > 0: if move_component.get_movement_direction() != 0.0:
parent.direction.x = 1 parent.transform.x.x = move_component.get_movement_direction()
elif parent.velocity.x < 0:
parent.direction.x = -1
parent.transform.x.x = parent.direction.x
if parent.is_on_floor(): if parent.is_on_floor():
return idle_state return idle_state

View File

@ -10,7 +10,8 @@ func enter() -> void:
.enter() .enter()
# parent.set_hurtbox(true) # parent.set_hurtbox(true)
parent.velocity.x = 0 parent.velocity.x = 0
print("Move Component: ", move_component.desired_movement_vector.x) if debug_state:
print(owner.name, " Move Component: ", move_component.desired_movement_vector.x)
func process_physics(delta: float) -> State: func process_physics(delta: float) -> State:
@ -24,7 +25,6 @@ func process_physics(delta: float) -> State:
if parent.velocity.x != 0.0: if parent.velocity.x != 0.0:
return move_state return move_state
if !parent.is_on_floor(): if !parent.is_on_floor():
return fall_state return fall_state
return null return null

View File

@ -12,13 +12,9 @@ func process_physics(delta: float) -> State:
if parent.velocity.x == 0.0: if parent.velocity.x == 0.0:
return idle_state return idle_state
else:
if parent.velocity.x > 0:
parent.direction.x = 1
elif parent.velocity.x < 0:
parent.direction.x = -1
parent.transform.x.x = parent.direction.x #Flip the character before tha actual move maybe.
parent.transform.x.x = move_component.get_movement_direction()
if !parent.is_on_floor(): if !parent.is_on_floor():
return fall_state return fall_state