game manager and data objects

This commit is contained in:
OddlyTimbot committed 2024-08-19 21:11:13 -04:00
1 parent e4be043988
commit 149ac083d4
38 files changed
+522 -122

No files matched your search

+18 -7
View File
@@ -13,6 +13,10 @@ extends CharacterBody2D
@onready var animated_sprite = $AnimatedSprite2D
signal deathComplete
var currentPlaying = "idle"
var faceLeft = false
var pushLeftEnabled = false
var pushRightEnabled = false
@@ -26,7 +30,8 @@ var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func die():
print("kill the player")
animated_sprite.play("death")
currentPlaying = "death"
animated_sprite.play(currentPlaying)
living = false
func _physics_process(delta):
@@ -74,27 +79,27 @@ func _physics_process(delta):
velocity.x = move_toward(velocity.x, 0, SPEED)
if is_on_floor():
if direction==0:
animated_sprite.play("idle")
currentPlaying = "idle"
animated_sprite.play(currentPlaying)
else:
animated_sprite.play("run")
currentPlaying="run"
animated_sprite.play(currentPlaying)
else:
animated_sprite.play("jump")
currentPlaying = "jump"
animated_sprite.play(currentPlaying)
move_and_slide()
if right_ray.is_colliding():
print("right ray is colliding")
var collider = right_ray.get_collider()
#what did we hit?
if collider is Node:
if collider.is_in_group("boxes"):
print("This is a pushable box")
pushRightEnabled = true
pushTarget = collider
else:
pushRightEnabled = false
if left_ray.is_colliding():
print("left ray is colliding")
var collider = left_ray.get_collider()
if collider is Node:
if collider.is_in_group("boxes"):
@@ -108,3 +113,9 @@ func _physics_process(delta):
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal()* BUMP_FORCE)
func _on_animation_finished():
if currentPlaying == "death":
print("Animation is Finished is death")
deathComplete.emit()
+23
View File
@@ -0,0 +1,23 @@
extends Node
var player: Resource
var coinCollectedTotal:int = 0
# Called when the node enters the scene tree for the first time.
func _ready():
print("GameManager is here!")
player = load("res://scripts/resources/player_stats.tres")
func coinCollected():
coinCollectedTotal +=1
print("coins collected "+str(coinCollectedTotal) )
func playerDamage():
player.health -= 20
print("Player health is now "+str(player.health) )
func resetPlayer():
player.health = player.max_health
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
+4 -3
View File
@@ -1,4 +1,4 @@
extends Area2D
class_name BadGuy extends Area2D
const speed = 60
var direction = 1
@@ -8,7 +8,8 @@ var direction = 1
@onready var right_floor_cast = $rightFloorCast
@onready var left_floor_cast = $leftFloorCast
@onready var sprite = $AnimatedSprite2D
@onready var game = %GameController
signal playerDamage
# Called when the node enters the scene tree for the first time.
func _ready():
@@ -37,4 +38,4 @@ func _process(delta):
func _on_body_entered(body):
if body.is_in_group("player"):
game.playerDeath()
playerDamage.emit()
+3 -13
View File
@@ -1,19 +1,9 @@
extends Area2D
@onready var game = %GameController
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
class_name Coin extends Area2D
signal coinCollected
func _on_body_entered(body):
if body.is_in_group("player"):
print("something hit the coin")
game.coinCollected()
coinCollected.emit()
queue_free()
+29 -13
View File
@@ -4,18 +4,27 @@ var myBox = preload("res://scenes/crate.tscn")
@onready var box_trap = $"../BoxTrap"
@onready var timer = $Timer
@onready var player = $"../CharacterBody2D"
@onready var coins = $"../Coins"
@onready var enemies = $"../enemies"
@onready var ui = $"../CanvasLayer/UI"
var totalCoins = 0
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
GameManager.resetPlayer()
for n in coins.get_children():
if n is Coin:
#wire up our listener
n.coinCollected.connect(_on_coin_coin_collected)
for n in enemies.get_children():
if n is BadGuy:
n.playerDamage.connect(playerDamage)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
if GameManager.player.health <= 0 && player.living:
player.die()
func _on_area_2d_areatrigger(effect, body):
@@ -32,13 +41,20 @@ func _on_area_2d_areatrigger(effect, body):
"powerup":
print("you got power")
func coinCollected():
totalCoins += 1
print("Game controller will add coin " + str(totalCoins) )
func playerDeath():
player.die()
timer.start()
func resetWorld():
get_tree().reload_current_scene()
func _on_coin_coin_collected():
GameManager.coinCollected()
ui.coinsUpdate(GameManager.coinCollectedTotal)
func playerDamage():
GameManager.playerDamage()
ui.healthUpdate(GameManager.player.health)
func _on_death_complete():
#This is where we reset the world
timer.start(.5)
+5
View File
@@ -0,0 +1,5 @@
class_name Character extends Resource
@export var max_health: int = 100
@export var starting_health: int = 100
@export var health: int = 100
+9
View File
@@ -0,0 +1,9 @@
[gd_resource type="Resource" script_class="Character" load_steps=2 format=3 uid="uid://bvy3omm4l72x2"]
[ext_resource type="Script" path="res://scripts/resources/character_stats.gd" id="1_m0sqn"]
[resource]
script = ExtResource("1_m0sqn")
max_health = 100
starting_health = 100
health = 100
+9
View File
@@ -0,0 +1,9 @@
[gd_resource type="Resource" script_class="Character" load_steps=2 format=3 uid="uid://d4d8akkkbbmh3"]
[ext_resource type="Script" path="res://scripts/resources/character_stats.gd" id="1_wlxgo"]
[resource]
script = ExtResource("1_wlxgo")
max_health = 50
starting_health = 50
health = 50
+12
View File
@@ -0,0 +1,12 @@
extends Control
@onready var health = $VBoxContainer/HBoxContainer/MarginContainer/Health
@onready var coins = $VBoxContainer/HBoxContainer/MarginContainer2/Coins
func _ready():
coinsUpdate(GameManager.coinCollectedTotal)
healthUpdate(GameManager.player.health)
func healthUpdate(amt):
health.text = "Health : "+str(amt)
func coinsUpdate(amt):
coins.text = "Coins : "+str(amt)