Minor style tweaks

This commit is contained in:
Aaron Franke
2020-02-12 15:30:42 -05:00
parent 77800b3f15
commit c117eb1847
8 changed files with 44 additions and 46 deletions

View File

@@ -1,8 +1,8 @@
extends KinematicBody2D
class_name Actor
extends KinematicBody2D
# Both the Player and Enemy inherit this scene as they have shared behaviours such as
# speed and are affected by gravity.
# Both the Player and Enemy inherit this scene as they have shared behaviours
# such as speed and are affected by gravity.
export var speed = Vector2(400.0, 500.0)

View File

@@ -1,13 +1,7 @@
extends Actor
class_name Enemy
extends Actor
onready var platform_detector = $PlatformDetector
onready var floor_detector_left = $FloorDetectorLeft
onready var floor_detector_right = $FloorDetectorRight
onready var sprite = $Sprite
onready var animation_player = $AnimationPlayer
enum State {
WALKING,
DEAD
@@ -15,6 +9,12 @@ enum State {
var _state = State.WALKING
onready var platform_detector = $PlatformDetector
onready var floor_detector_left = $FloorDetectorLeft
onready var floor_detector_right = $FloorDetectorRight
onready var sprite = $Sprite
onready var animation_player = $AnimationPlayer
# This function is called when the scene enters the scene tree.
# We can initialize variables here.
func _ready():
@@ -29,12 +29,12 @@ func _ready():
# 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.
# 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)
@@ -49,16 +49,9 @@ func _physics_process(_delta):
animation_player.play(animation)
func destroy():
_state = State.DEAD
_velocity = Vector2.ZERO
# 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
):
func calculate_move_velocity(linear_velocity):
var velocity = linear_velocity
if not floor_detector_left.is_colliding():
@@ -72,6 +65,11 @@ func calculate_move_velocity(
return velocity
func destroy():
_state = State.DEAD
_velocity = Vector2.ZERO
func get_new_animation():
var animation_new = ""
if _state == State.WALKING:

View File

@@ -1,15 +1,15 @@
extends Position2D
class_name Gun
extends Position2D
# Represents a weapon that spawns and shoots bullets.
# The Cooldown timer controls the cooldown duration between shots.
const BULLET_VELOCITY = 1000.0
const Bullet = preload("res://src/Objects/Bullet.tscn")
onready var sound_shoot = $Shoot
onready var timer = $Cooldown
const Bullet = preload("res://src/Objects/Bullet.tscn")
const BULLET_VELOCITY = 1000.0
func shoot(direction = 1):
if not timer.is_stopped():

View File

@@ -1,5 +1,5 @@
extends Actor
class_name Player
extends Actor
const FLOOR_DETECT_DISTANCE = 40.0
@@ -23,12 +23,12 @@ onready var gun = $Sprite/Gun
# 5. Shoots bullets.
# 6. 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.
# 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):
var direction = get_direction()
@@ -38,7 +38,7 @@ func _physics_process(_delta):
var snap_vector = Vector2.DOWN * FLOOR_DETECT_DISTANCE if direction.y == 0.0 else Vector2.ZERO
var is_on_platform = platform_detector.is_colliding()
_velocity = move_and_slide_with_snap(
_velocity, snap_vector, FLOOR_NORMAL, not is_on_platform, 4, 0.9, false
_velocity, snap_vector, FLOOR_NORMAL, not is_on_platform, 4, 0.9, false
)
# When the characters direction changes, we want to to scale the Sprite accordingly to flip it.

View File

@@ -1,6 +1,6 @@
extends Node
# This class contains controls that should always be accessible, like pausing
# the game or toggling the window full-screen.
extends Node
# The "_" prefix is a convention to indicate that variables are private,

View File

@@ -1,5 +1,5 @@
extends RigidBody2D
class_name Bullet
extends RigidBody2D
onready var animation_player = $AnimationPlayer

View File

@@ -1,5 +1,5 @@
extends Area2D
class_name Coin
extends Area2D
# Collectible that disappears when the player touches it.
onready var animation_player = $AnimationPlayer
@@ -7,9 +7,9 @@ onready var animation_player = $AnimationPlayer
# The Coins only detects collisions with the Player thanks to its collision mask.
# This prevents other characters such as enemies from picking up coins.
# # When the player collides with a coin, the coin plays its 'picked' animation.
# The animation takes cares of making the coin disappear, but also deactivates its collisions
# and frees it from memory, saving us from writing more complex code.
# When the player collides with a coin, the coin plays its 'picked' animation.
# The animation takes cares of making the coin disappear, but also deactivates its
# collisions and frees it from memory, saving us from writing more complex code.
# Click the AnimationPlayer node to see the animation timeline.
func _on_body_entered(_body):
animation_player.play("picked")

View File

@@ -8,15 +8,15 @@ func _ready():
visible = false
func close():
visible = false
func open():
visible = true
resume_button.grab_focus()
func close():
visible = false
func _on_ResumeButton_pressed():
get_tree().paused = false
visible = false