Skip to content

Commit

Permalink
refactored Enemy, Coin and Bullet
Browse files Browse the repository at this point in the history
  • Loading branch information
cogwoz committed Nov 13, 2019
1 parent c7de3b4 commit c33f9ff
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 160 deletions.
6 changes: 5 additions & 1 deletion platformer-2d-rework/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/Actors/Gun.gd"
}, {
"base": "Node2D",
"base": "KinematicBody2D",
"class": "MovingPlatform",
"language": "GDScript",
"path": "res://src/Platforms/MovingPlatform.gd"
Expand Down Expand Up @@ -129,7 +129,11 @@ spawn={

[layer_names]

2d_physics/layer_1="player"
2d_physics/layer_2="enemies"
2d_physics/layer_3="coins"
2d_physics/layer_4="platforms"
2d_physics/layer_5="ground"

[physics]

Expand Down
4 changes: 2 additions & 2 deletions platformer-2d-rework/src/Actors/Actor.gd
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
extends KinematicBody2D

class_name Actor

const FLOOR_NORMAL: = Vector2.UP

export var speed: = Vector2(400.0, 500.0)
export var gravity: = 3500.0

const FLOOR_NORMAL: = Vector2.UP

var _velocity: = Vector2.ZERO


Expand Down
82 changes: 60 additions & 22 deletions platformer-2d-rework/src/Actors/Enemy.gd
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
100 changes: 59 additions & 41 deletions platformer-2d-rework/src/Actors/Enemy.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
[ext_resource path="res://assets/art/enemy/enemy.png" type="Texture" id=4]
[ext_resource path="res://src/Actors/Enemy.gd" type="Script" id=5]

[sub_resource type="Animation" id=1]
resource_name = "explode"
length = 5.0
step = 0.0
[sub_resource type="CanvasItemMaterial" id=1]

[sub_resource type="Animation" id=2]
resource_name = "destroy"
length = 1.5
tracks/0/type = "value"
tracks/0/path = NodePath("Sprite:frame")
tracks/0/interp = 1
Expand All @@ -20,7 +21,7 @@ tracks/0/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
"update": 0,
"values": [ 0 ]
"values": [ 7 ]
}
tracks/1/type = "value"
tracks/1/path = NodePath("Sprite:rotation_degrees")
Expand All @@ -29,8 +30,8 @@ tracks/1/loop_wrap = true
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/keys = {
"times": PoolRealArray( 0, 3.2 ),
"transitions": PoolRealArray( 0.5, 1 ),
"times": PoolRealArray( 0, 0.8 ),
"transitions": PoolRealArray( 0.0796601, 1 ),
"update": 0,
"values": [ 0.0, 180.0 ]
}
Expand All @@ -41,10 +42,10 @@ tracks/2/loop_wrap = true
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/keys = {
"times": PoolRealArray( 2.6, 3.4 ),
"transitions": PoolRealArray( 1, 1 ),
"times": PoolRealArray( 0, 0.3, 0.9 ),
"transitions": PoolRealArray( 1, 1, 1 ),
"update": 0,
"values": [ Color( 1, 1, 1, 1 ), Color( 1, 1, 1, 0 ) ]
"values": [ Color( 1, 1, 1, 1 ), Color( 1, 1, 1, 1 ), Color( 1, 1, 1, 0 ) ]
}
tracks/3/type = "value"
tracks/3/path = NodePath("Explosion:emitting")
Expand All @@ -53,7 +54,7 @@ tracks/3/loop_wrap = true
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/keys = {
"times": PoolRealArray( 0, 2.8 ),
"times": PoolRealArray( 0, 0.8 ),
"transitions": PoolRealArray( 1, 1 ),
"update": 1,
"values": [ false, true ]
Expand All @@ -65,13 +66,13 @@ tracks/4/loop_wrap = true
tracks/4/imported = false
tracks/4/enabled = true
tracks/4/keys = {
"times": PoolRealArray( 2.8, 3.4 ),
"times": PoolRealArray( 0.4, 0.8 ),
"transitions": PoolRealArray( 1, 1 ),
"update": 0,
"values": [ 1.0, 4.0 ]
}
tracks/5/type = "value"
tracks/5/path = NodePath("SoundHit:playing")
tracks/5/path = NodePath("Hit:playing")
tracks/5/interp = 1
tracks/5/loop_wrap = true
tracks/5/imported = false
Expand All @@ -83,13 +84,13 @@ tracks/5/keys = {
"values": [ true ]
}
tracks/6/type = "value"
tracks/6/path = NodePath("SoundExplode:playing")
tracks/6/path = NodePath("Explode:playing")
tracks/6/interp = 1
tracks/6/loop_wrap = true
tracks/6/imported = false
tracks/6/enabled = true
tracks/6/keys = {
"times": PoolRealArray( 2.9 ),
"times": PoolRealArray( 0.6 ),
"transitions": PoolRealArray( 1 ),
"update": 1,
"values": [ true ]
Expand All @@ -101,7 +102,7 @@ tracks/7/loop_wrap = true
tracks/7/imported = false
tracks/7/enabled = true
tracks/7/keys = {
"times": PoolRealArray( 4.2 ),
"times": PoolRealArray( 1.5 ),
"transitions": PoolRealArray( 1 ),
"values": [ {
"args": [ ],
Expand All @@ -120,8 +121,20 @@ tracks/8/keys = {
"update": 1,
"values": [ 0 ]
}
tracks/9/type = "value"
tracks/9/path = NodePath("Sprite:position")
tracks/9/interp = 1
tracks/9/loop_wrap = true
tracks/9/imported = false
tracks/9/enabled = true
tracks/9/keys = {
"times": PoolRealArray( 0, 0.2, 0.4 ),
"transitions": PoolRealArray( 2.2974, 0.183011, 1 ),
"update": 0,
"values": [ Vector2( 0, 0 ), Vector2( 0, -32 ), Vector2( 0, 0 ) ]
}

[sub_resource type="Animation" id=2]
[sub_resource type="Animation" id=3]
length = 6.75
loop = true
step = 0.25
Expand All @@ -138,8 +151,7 @@ tracks/0/keys = {
"values": [ 5, 6, 5, 6, 5, 6, 7, 6, 7, 5 ]
}

[sub_resource type="Animation" id=3]
resource_name = "walk"
[sub_resource type="Animation" id=4]
length = 1.25
loop = true
step = 0.25
Expand All @@ -156,8 +168,6 @@ tracks/0/keys = {
"values": [ 0, 1, 2, 3, 4, 0 ]
}

[sub_resource type="CanvasItemMaterial" id=4]

[sub_resource type="CapsuleShape2D" id=5]
radius = 13.4556
height = 14.2002
Expand All @@ -170,41 +180,49 @@ flag_disable_z = true
gravity = Vector3( 0, 98, 0 )
orbit_velocity = 0.0
orbit_velocity_random = 0.0
scale = 4.0

[node name="Enemy" type="KinematicBody2D"]
collision_layer = 2
collision_mask = 4
collision_mask = 24
script = ExtResource( 5 )
speed = Vector2( 150, 500 )
gravity = 1800.0

[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
anims/explode = SubResource( 1 )
anims/idle = SubResource( 2 )
anims/walk = SubResource( 3 )
[node name="PlatformDetector" type="RayCast2D" parent="."]
position = Vector2( 0, 12 )
enabled = true
cast_to = Vector2( 0, 8 )
collision_mask = 8

[node name="FloorDetectorLeft" type="RayCast2D" parent="."]
position = Vector2( -30, -8 )
enabled = true
cast_to = Vector2( 0, 70 )
collision_mask = 24

[node name="FloorDetectorRight" type="RayCast2D" parent="."]
position = Vector2( 31.16, -8 )
enabled = true
cast_to = Vector2( 0, 70 )
collision_mask = 24

[node name="Sprite" type="Sprite" parent="."]
material = SubResource( 4 )
material = SubResource( 1 )
texture = ExtResource( 4 )
flip_h = true
hframes = 8
frame = 5

[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
anims/destroy = SubResource( 2 )
anims/idle = SubResource( 3 )
anims/walk = SubResource( 4 )

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
rotation = -1.5708
shape = SubResource( 5 )

[node name="DetectFloorLeft" type="RayCast2D" parent="."]
position = Vector2( -32, -10 )
enabled = true
cast_to = Vector2( 0, 70 )
collision_mask = 4

[node name="DetectFloorRight" type="RayCast2D" parent="."]
position = Vector2( 32, -10 )
enabled = true
cast_to = Vector2( 0, 70 )
collision_mask = 4

[node name="Explosion" type="Particles2D" parent="."]
self_modulate = Color( 1, 1, 1, 0.12 )
material = SubResource( 6 )
Expand All @@ -217,8 +235,8 @@ draw_order = 215832976
process_material = SubResource( 7 )
texture = ExtResource( 1 )

[node name="SoundHit" type="AudioStreamPlayer2D" parent="."]
[node name="Hit" type="AudioStreamPlayer2D" parent="."]
stream = ExtResource( 3 )

[node name="SoundExplode" type="AudioStreamPlayer2D" parent="."]
[node name="Explode" type="AudioStreamPlayer2D" parent="."]
stream = ExtResource( 2 )
3 changes: 2 additions & 1 deletion platformer-2d-rework/src/Actors/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ At a glance, you can see that the physics process loop:
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:
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.
"""
Expand Down
4 changes: 2 additions & 2 deletions platformer-2d-rework/src/Actors/Player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ tracks/0/keys = {
height = 44.4787

[node name="Player" type="KinematicBody2D"]
collision_mask = 7
collision_mask = 30
script = ExtResource( 1 )
speed = Vector2( 400, 700 )
gravity = 1800.0
Expand All @@ -207,7 +207,7 @@ collision_mask = 8
texture = ExtResource( 2 )
vframes = 2
hframes = 16
frame = 26
frame = 22

[node name="Gun" type="Position2D" parent="Sprite"]
position = Vector2( 30.6589, 6.13176 )
Expand Down
Loading

0 comments on commit c33f9ff

Please sign in to comment.