-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
285 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,82 @@ | ||
extends Actor | ||
|
||
class_name Enemy | ||
|
||
|
||
onready var platform_detector: RayCast2D = $PlatformDetector | ||
onready var floor_detector_left: RayCast2D = $FloorDetectorLeft | ||
onready var floor_detector_right: RayCast2D = $FloorDetectorRight | ||
onready var sprite: Sprite = $Sprite | ||
onready var animation_player: AnimationPlayer = $AnimationPlayer | ||
|
||
onready var DetectFloorLeft: RayCast2D = $DetectFloorLeft | ||
onready var DetectFloorRight: RayCast2D = $DetectFloorRight | ||
|
||
enum State {WALKING, DEAD} | ||
|
||
var state = State.WALKING | ||
|
||
var _current_animation := "" | ||
|
||
var _state = State.WALKING | ||
|
||
""" | ||
This function is called when the scene enters the scene tree. | ||
We can initialize variables here. | ||
""" | ||
func _ready(): | ||
_velocity.x = -speed.x | ||
_velocity.x = speed.x | ||
|
||
""" | ||
Physics process is a built-in loop in Godot. | ||
If you define _physics_process on a node, Godot will call it every frame. | ||
At a glance, you can see that the physics process loop: | ||
1. Calculates the move velocity. | ||
2. Moves the character. | ||
3. Updates the sprite direction. | ||
4. Updates the animation. | ||
Splitting the physics process logic into functions not only makes it easier to read, it help to change or improve the code later on: | ||
- If you need to change a calculation, you can use Go To -> Function (Ctrl Alt F) to quickly jump to the corresponding function. | ||
- If you split the character into a state machine or more advanced pattern, you can easily move individual functions. | ||
""" | ||
func _physics_process(delta): | ||
_velocity = calculate_move_velocity(_velocity) | ||
|
||
_velocity.x *= -1 if is_on_wall() or not DetectFloorLeft.is_colliding() or not DetectFloorRight.is_colliding() else 1 | ||
|
||
# We only update the y value of _velocity as we want to handle the horizontal movement ourselves. | ||
_velocity.y = move_and_slide(_velocity, FLOOR_NORMAL).y | ||
|
||
# We flip the Sprite depending on which way the enemy is moving. | ||
sprite.scale.x = 1 if _velocity.x > 0 else -1 | ||
|
||
var new_animation = "idle" | ||
|
||
var animation: = get_new_animation() | ||
if animation != animation_player.current_animation: | ||
animation_player.play(animation) | ||
|
||
if state == State.WALKING: | ||
new_animation = "walk" | ||
else: | ||
_velocity = Vector2.ZERO | ||
new_animation = "explode" | ||
|
||
if _current_animation != new_animation: | ||
_current_animation = new_animation | ||
animation_player.play(_current_animation) | ||
func destroy(): | ||
_state = State.DEAD | ||
_velocity = Vector2.ZERO | ||
|
||
|
||
func hit_by_bullet(): | ||
state = State.DEAD | ||
""" | ||
This function calculates a new velocity whenever you need it. | ||
If the enemy encounters a wall or an edge, the horizontal velocity is flipped. | ||
""" | ||
func calculate_move_velocity( | ||
linear_velocity: Vector2 | ||
) -> Vector2: | ||
var velocity: = linear_velocity | ||
|
||
if not floor_detector_left.is_colliding(): | ||
velocity.x = speed.x | ||
elif not floor_detector_right.is_colliding(): | ||
velocity.x = -speed.x | ||
|
||
if is_on_wall(): | ||
velocity.x *= -1 | ||
|
||
return velocity | ||
|
||
|
||
func get_new_animation() -> String: | ||
var animation_new: = "" | ||
if _state == State.WALKING: | ||
animation_new = "walk" if abs(_velocity.x) > 0 else "idle" | ||
else: | ||
animation_new = "destroy" | ||
return animation_new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.