we add simple enemy ai and added gamemanager

This commit is contained in:
KieranScott committed 2024-09-30 21:14:46 -04:00
1 parent 8ce6a444b7
commit 9a70b862ec
19 files changed
+540 -8

No files matched your search

@@ -0,0 +1,5 @@
class_name CharacterStats extends Resource
@export var max_health:int = 100
@export var starting_health:int = 100
@export var current_health:int = 100
@@ -0,0 +1,9 @@
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://sru8awuj2bof"]
[ext_resource type="Script" path="res://Sripts/Resources/character_stats.gd" id="1_u5owe"]
[resource]
script = ExtResource("1_u5owe")
max_health = 100
starting_health = 100
current_heath = 100
@@ -0,0 +1,9 @@
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://b7sfi2frc80hf"]
[ext_resource type="Script" path="res://Sripts/Resources/character_stats.gd" id="1_arx74"]
[resource]
script = ExtResource("1_arx74")
max_health = 50
starting_health = 50
current_heath = 50
+17
View File
@@ -0,0 +1,17 @@
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,24 @@
extends Node
var coinscollected:int = 0
var player:Resource
signal playerdeath
func _ready() -> void:
print("Game Controller is on")
player = load("res://Sripts/Resources/player.tres")
func coincollected():
coinscollected +=1
print("Gamecontroller Coin Collected : "+str(coinscollected) )
func resetplayer():
player.current_health = player.max_health
func playerdamaged():
player.current_health -= 20
print("player health is : "+str(player.current_health))
# is player dead
if player.current_health <=0:
#Death
playerdeath.emit()
+5 -1
View File
@@ -19,7 +19,11 @@ var pushTarget
var bullet = preload("res://Scenes/Bullet.tscn")
func killplayer():
print("dying")
#play death
playerSprite.play("Death")
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
@@ -5,6 +5,10 @@ var bulletPool:Array = []
# crate pool thing
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 deathdelay: Timer = $Timer
func _ready() -> void:
# pool crates
@@ -12,6 +16,18 @@ func _ready() -> void:
if obj.is_in_group("Pushable"):
cratePool.push_back(obj)
print("Total crate in play: "+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.playerdamaged)
# gamecontroller signals
GameController.playerdeath.connect(killplayer)
func boxTrap():
print("boxTrap active")
@@ -46,3 +62,12 @@ func crateFactory():
var myCrate = crate.instantiate()
myCrate.add_to_group("Pushable")
return myCrate
func killplayer():
print("kill player")
player.killplayer()
deathdelay.start(1)
func resetworld():
GameController.resetplayer()
get_tree().reload_current_scene()
+39
View File
@@ -0,0 +1,39 @@
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
position.x += direction * speed * delta
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
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("Player"):
playerdamage.emit()