gamemanager, ui, and more!
This commit is contained in:
1 parent
ee3464597a
commit
b0a6ef603e
117 files changed
+556
-134
No files matched your search
@@ -12,6 +12,9 @@ extends CharacterBody2D
|
||||
@onready var marker_left = $MarkerLeft
|
||||
@onready var animated_sprite = $AnimatedSprite2D
|
||||
|
||||
signal deathComplete
|
||||
var currentPlaying = "idle"
|
||||
|
||||
var faceLeft = false
|
||||
var pushLeftEnabled = false
|
||||
var pushRightEnabled = false
|
||||
@@ -26,7 +29,8 @@ var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
||||
|
||||
func die():
|
||||
print("You died!")
|
||||
animated_sprite.play("death")
|
||||
currentPlaying = "death"
|
||||
animated_sprite.play(currentPlaying)
|
||||
living = false
|
||||
|
||||
func _physics_process(delta):
|
||||
@@ -39,7 +43,6 @@ func _physics_process(delta):
|
||||
velocity.y = JUMP_VELOCITY
|
||||
# Hand force push (melee attack)
|
||||
if Input.is_action_just_pressed("push") && pushRightEnabled && faceLeft == false:
|
||||
print("I should force push")
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 10)
|
||||
pushRightEnabled = false
|
||||
if Input.is_action_just_pressed("push") && pushLeftEnabled:
|
||||
@@ -79,27 +82,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"):
|
||||
@@ -113,3 +116,7 @@ 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":
|
||||
deathComplete.emit()
|
||||
@@ -0,0 +1,25 @@
|
||||
extends Node
|
||||
|
||||
var coinCollectedTotal:int = 0
|
||||
var player: Resource
|
||||
|
||||
|
||||
# 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
|
||||
+5
-5
@@ -1,14 +1,15 @@
|
||||
extends Area2D
|
||||
class_name BadGuy extends Area2D
|
||||
|
||||
@export var speed = 60.0
|
||||
var direction = 1
|
||||
|
||||
@onready var sprite = $AnimatedSprite2D
|
||||
@onready var right_cast = $RightCast
|
||||
@onready var left_cast = $LeftCast
|
||||
@onready var sprite = $AnimatedSprite2D
|
||||
@onready var right_floor_cast = $RightFloorCast
|
||||
@onready var left_floor_cast = $LeftFloorCast
|
||||
@onready var game = %GameController
|
||||
|
||||
signal playerDamage
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
@@ -34,7 +35,6 @@ func _process(delta):
|
||||
#movement
|
||||
position.x += direction * speed * delta
|
||||
|
||||
|
||||
func _on_body_entered(body):
|
||||
if body.is_in_group("player"):
|
||||
game.playerDeath()
|
||||
playerDamage.emit()
|
||||
+3
-13
@@ -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("somebody entered a coin")
|
||||
game.coinCollected()
|
||||
coinCollected.emit()
|
||||
queue_free()
|
||||
+31
-13
@@ -5,17 +5,29 @@ var myBox = preload("res://scenes/crate.tscn")
|
||||
@onready var box_trap = $"../BoxTrap"
|
||||
@onready var timer = $Timer
|
||||
@onready var player = $"../CharacterBody2D"
|
||||
|
||||
var totalCoins = 0
|
||||
@onready var coins = $"../Coins"
|
||||
@onready var enemies = $"../Enemies"
|
||||
@onready var ui = $"../CanvasLayer/UI"
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
|
||||
GameManager.resetPlayer()
|
||||
ui.coinsUpdate(GameManager.coinCollectedTotal)
|
||||
ui.healthUpdate(GameManager.player.health)
|
||||
#the above might not always work, and might return an error due to loading orders;
|
||||
#if that happens, uncomment the initialization versions in the ui script itself
|
||||
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 and player.living:
|
||||
player.die()
|
||||
|
||||
func _on_area_2d_areatrigger(effect, body):
|
||||
print("Game Controller sees the trigger " + effect)
|
||||
@@ -29,13 +41,19 @@ func _on_area_2d_areatrigger(effect, body):
|
||||
owner.add_child(box)
|
||||
box.transform = box_trap.transform
|
||||
|
||||
func coinCollected():
|
||||
totalCoins += 1
|
||||
print("I have " + str(totalCoins) + " coins")
|
||||
|
||||
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():
|
||||
if player.living:
|
||||
GameManager.playerDamage()
|
||||
ui.healthUpdate(GameManager.player.health)
|
||||
|
||||
|
||||
func _on_death_complete():
|
||||
#this is where we start the timer that will, in turn, reset the world
|
||||
timer.start(0.5)
|
||||
@@ -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
|
||||
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="Character" load_steps=2 format=3 uid="uid://whu7qibv7bmy"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/resources/character_stats.gd" id="1_26k3c"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_26k3c")
|
||||
max_health = 100
|
||||
starting_health = 100
|
||||
health = 100
|
||||
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="Character" load_steps=2 format=3 uid="uid://dbf2ykast7vdv"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/resources/character_stats.gd" id="1_72f0x"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_72f0x")
|
||||
max_health = 50
|
||||
starting_health = 50
|
||||
health = 50
|
||||
@@ -0,0 +1,14 @@
|
||||
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)
|
||||
Reference in new issue
Block a user