week 5
This commit is contained in:
1 parent
f8cfa904d9
commit
42eb81d7d4
19 files changed
+537
-6
No files matched your search
@@ -0,0 +1,38 @@
|
||||
class_name Enemy extends Area2D
|
||||
|
||||
signal playerDamage
|
||||
@onready var cast_right: RayCast2D = $CastRight
|
||||
@onready var cast_left: RayCast2D = $CastLeft
|
||||
@onready var cast_floor_right: RayCast2D = $CastFloorRight
|
||||
@onready var cast_floor_left: RayCast2D = $CastFloorLeft
|
||||
const speed = 60
|
||||
var direction = 1
|
||||
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
if cast_right.is_colliding():
|
||||
if not cast_right.get_collider().is_in_group("player"):
|
||||
direction = -1
|
||||
sprite.flip_h = true
|
||||
if cast_left.is_colliding():
|
||||
if not cast_left.get_collider().is_in_group("player"):
|
||||
direction = 1
|
||||
sprite.flip_h = false
|
||||
if not cast_floor_right.is_colliding():
|
||||
direction = -1
|
||||
sprite.flip_h = true
|
||||
if not cast_floor_left.is_colliding():
|
||||
direction = 1
|
||||
sprite.flip_h = false
|
||||
position.x += direction * speed * delta
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if body.is_in_group("player"):
|
||||
playerDamage.emit()
|
||||
@@ -0,0 +1,18 @@
|
||||
extends Area2D
|
||||
|
||||
signal coinCollected
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if body.is_in_group("player"):
|
||||
coinCollected.emit()
|
||||
queue_free()
|
||||
@@ -0,0 +1,27 @@
|
||||
extends Node
|
||||
|
||||
var coinsCollected:int = 0
|
||||
var player:Resource
|
||||
|
||||
signal playerDeath
|
||||
|
||||
func _ready() -> void:
|
||||
print("Game controller is ready")
|
||||
player = load("res://scripts/resources/player.tres")
|
||||
|
||||
|
||||
func coinCollected():
|
||||
coinsCollected +=1
|
||||
print("Game Controller Coin Collected : "+str(coinsCollected))
|
||||
|
||||
func resetPlayer():
|
||||
player.health = player.max_health
|
||||
|
||||
func playerDamage():
|
||||
player.health -= 20
|
||||
print("Player health is : "+str(player.health) )
|
||||
#is the player dead?
|
||||
if player.health <= 0:
|
||||
#kill him
|
||||
playerDeath.emit()
|
||||
|
||||
@@ -19,7 +19,11 @@ var pushTarget
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
|
||||
|
||||
func killPlayer():
|
||||
print("kill the player")
|
||||
#play death animation
|
||||
playerSprite.play("death")
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class_name CharacterStats extends Resource
|
||||
|
||||
@export var max_health:int = 100
|
||||
@export var starting_health: int = 100
|
||||
@export var health: int = 100
|
||||
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://koxvwi31cjtf"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/resources/character_stats.gd" id="1_mwxs7"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_mwxs7")
|
||||
max_health = 100
|
||||
starting_health = 100
|
||||
health = 100
|
||||
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://edb4xjspowwc"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/resources/character_stats.gd" id="1_6yxqy"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_6yxqy")
|
||||
max_health = 50
|
||||
starting_health = 50
|
||||
health = 50
|
||||
@@ -5,6 +5,10 @@ var bulletPool:Array = []
|
||||
# crate stuff
|
||||
var cratePool:Array = []
|
||||
@onready var box_trap_target: Node2D = $"../BoxTrapTarget"
|
||||
@onready var coins: Node = $"../coins"
|
||||
@onready var enemies: Node = $"../enemies"
|
||||
@onready var player: CharacterBody2D = $"../CharacterBody2D"
|
||||
@onready var timer: Timer = $Timer
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
@@ -13,6 +17,17 @@ func _ready() -> void:
|
||||
if obj.is_in_group("pushables"):
|
||||
cratePool.push_back(obj)
|
||||
print("Total crates on screen : "+str(cratePool.size() ) )
|
||||
for n in coins.get_children():
|
||||
#hook up the coinCollected signal
|
||||
print("coin found")
|
||||
n.coinCollected.connect(GameController.coinCollected)
|
||||
# Enemy routine
|
||||
for n in enemies.get_children():
|
||||
if n is Enemy:
|
||||
#wire up the signal
|
||||
n.playerDamage.connect(GameController.playerDamage)
|
||||
#GameController signals
|
||||
GameController.playerDeath.connect(killPlayer)
|
||||
|
||||
func boxTrap():
|
||||
print("Trigger a box trap!")
|
||||
@@ -47,3 +62,13 @@ func crateFactory():
|
||||
var myCrate = crate.instantiate()
|
||||
myCrate.add_to_group("pushables")
|
||||
return myCrate
|
||||
|
||||
func killPlayer():
|
||||
print("kill the player now")
|
||||
player.killPlayer()
|
||||
timer.start(3)
|
||||
|
||||
|
||||
func resetWorld():
|
||||
GameController.resetPlayer()
|
||||
get_tree().reload_current_scene()
|
||||
Reference in new issue
Block a user