player death, player hurt, ui
This commit is contained in:
1 parent
7c292f7cec
commit
b25214ebfe
14 files changed
+446
-102
No files matched your search
@@ -3,7 +3,7 @@ class_name Bullet extends Area2D
|
||||
|
||||
var speed = 700
|
||||
signal hit
|
||||
|
||||
signal badguyHit(area)
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
@@ -27,3 +27,10 @@ func _on_body_entered(body: Node2D) -> void:
|
||||
print("Bullet hits something")
|
||||
if not body.is_in_group("player"):
|
||||
hit.emit(self, body)
|
||||
|
||||
|
||||
func _on_area_entered(area: Area2D) -> void:
|
||||
print("Bullet hitting area")
|
||||
if area.is_in_group("enemies"):
|
||||
badguyHit.emit(area)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
|
||||
const RANGE = 400.0
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
const BUMP_FORCE = 50
|
||||
@@ -15,6 +15,8 @@ var pushLeftEnabled = false
|
||||
@export var PUSH_FORCE = 700
|
||||
@onready var animation: AnimatedSprite2D = $AnimatedSprite2D
|
||||
@onready var teleportZone: Area2D = $"../Area2D"
|
||||
var teleportCounter = 0
|
||||
|
||||
|
||||
# Bullet Attack Variables
|
||||
@onready var right_target: Node2D = $RightTarget
|
||||
@@ -22,96 +24,128 @@ var pushLeftEnabled = false
|
||||
|
||||
var isJumping = false
|
||||
var safeTeleport = false
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
else:
|
||||
isJumping = false
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
animation.play("jump")
|
||||
isJumping = true
|
||||
# Hover
|
||||
if not is_on_floor() and Input.is_action_pressed("hover"):
|
||||
velocity.x = 0
|
||||
velocity.y = 0
|
||||
# Shove Attack
|
||||
if Input.is_action_just_pressed("shove"):
|
||||
if pushRightEnabled && faceLeft == false:
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
|
||||
if pushLeftEnabled && faceLeft == true:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
|
||||
# Teleport
|
||||
if Input.is_action_just_pressed("teleport"):
|
||||
while not safeTeleport:
|
||||
teleportZone.position = Vector2(position.x+randf_range(-300,300),position.y+randf_range(-300,300))
|
||||
if teleportZone.has_overlapping_bodies():
|
||||
safeTeleport = false
|
||||
else:
|
||||
safeTeleport = true
|
||||
safeTeleport= false
|
||||
position = Vector2(teleportZone.position.x,teleportZone.position.y)
|
||||
|
||||
|
||||
|
||||
# Handle Shooting
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
#print("I want to shoot")
|
||||
#make a new bullet
|
||||
|
||||
|
||||
|
||||
if faceLeft == true:
|
||||
var myBullet = %SceneManager.makeBullet(left_target.global_transform,-700)
|
||||
#myBullet.transform = left_target.global_transform
|
||||
#myBullet.setSpeed(-700)
|
||||
if faceLeft == false:
|
||||
var myBullet = %SceneManager.makeBullet(right_target.global_transform,700)
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var direction := Input.get_axis("left", "right")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
if direction <0:
|
||||
if not isJumping:
|
||||
animation.play("run")
|
||||
faceLeft = true
|
||||
animation.flip_h = true
|
||||
if direction >0:
|
||||
if not isJumping:
|
||||
animation.play("run")
|
||||
faceLeft = false
|
||||
animation.flip_h = false
|
||||
else:
|
||||
if not isJumping:
|
||||
animation.play("idle")
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
if right_ray.is_colliding():
|
||||
# print("Right ray contact!")
|
||||
if not faceLeft:
|
||||
var collider = right_ray.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
pushTarget = collider
|
||||
pushRightEnabled = true
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
|
||||
if left_ray.is_colliding():
|
||||
if faceLeft:
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
pushTarget = collider
|
||||
pushLeftEnabled = true
|
||||
else:
|
||||
pushLeftEnabled = false
|
||||
|
||||
var living = true
|
||||
var animPlaying = "idle"
|
||||
|
||||
signal deathAnimationComplete
|
||||
|
||||
|
||||
func hurtPlayer(_amt):
|
||||
if _amt < 100:
|
||||
animPlaying = "hurt"
|
||||
animation.play(animPlaying)
|
||||
|
||||
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * BUMP_FORCE)
|
||||
func killPlayer():
|
||||
print("PC will kill player")
|
||||
if living:
|
||||
living = false
|
||||
animPlaying = "death"
|
||||
animation.play(animPlaying)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if living and not animPlaying == "hurt":
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
else:
|
||||
isJumping = false
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
animation.play("jump")
|
||||
isJumping = true
|
||||
# Hover
|
||||
if not is_on_floor() and Input.is_action_pressed("hover"):
|
||||
velocity.x = 0
|
||||
velocity.y = 0
|
||||
# Shove Attack
|
||||
if Input.is_action_just_pressed("shove"):
|
||||
if pushRightEnabled && faceLeft == false:
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
|
||||
if pushLeftEnabled && faceLeft == true:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
|
||||
# Teleport
|
||||
if Input.is_action_just_pressed("teleport"):
|
||||
while not safeTeleport and teleportCounter < 100:
|
||||
teleportZone.position = Vector2(position.x+randf_range(-RANGE,RANGE),position.y+randf_range(-RANGE,RANGE))
|
||||
if teleportZone.has_overlapping_bodies():
|
||||
safeTeleport = false
|
||||
teleportCounter +=1
|
||||
else:
|
||||
safeTeleport = true
|
||||
teleportCounter +=1
|
||||
safeTeleport= false
|
||||
teleportCounter = 0
|
||||
position = Vector2(teleportZone.position.x,teleportZone.position.y)
|
||||
|
||||
|
||||
|
||||
# Handle Shooting
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
#print("I want to shoot")
|
||||
#make a new bullet
|
||||
|
||||
|
||||
|
||||
if faceLeft == true:
|
||||
var myBullet = %SceneManager.makeBullet(left_target.global_transform,-700)
|
||||
#myBullet.transform = left_target.global_transform
|
||||
#myBullet.setSpeed(-700)
|
||||
if faceLeft == false:
|
||||
var myBullet = %SceneManager.makeBullet(right_target.global_transform,700)
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var direction := Input.get_axis("left", "right")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
if direction <0:
|
||||
if not isJumping:
|
||||
animation.play("run")
|
||||
faceLeft = true
|
||||
animation.flip_h = true
|
||||
if direction >0:
|
||||
if not isJumping:
|
||||
animation.play("run")
|
||||
faceLeft = false
|
||||
animation.flip_h = false
|
||||
else:
|
||||
if not isJumping:
|
||||
animation.play("idle")
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
if right_ray.is_colliding():
|
||||
# print("Right ray contact!")
|
||||
if not faceLeft:
|
||||
var collider = right_ray.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
pushTarget = collider
|
||||
pushRightEnabled = true
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
|
||||
if left_ray.is_colliding():
|
||||
if faceLeft:
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
pushTarget = collider
|
||||
pushLeftEnabled = true
|
||||
else:
|
||||
pushLeftEnabled = false
|
||||
|
||||
|
||||
|
||||
for i in get_slide_collision_count():
|
||||
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() -> void:
|
||||
if animPlaying == "hurt":
|
||||
animPlaying = "idle"
|
||||
if animPlaying == "death":
|
||||
# emit death signal
|
||||
deathAnimationComplete.emit()
|
||||
|
||||
@@ -13,13 +13,17 @@ var player:Resource
|
||||
|
||||
#level information
|
||||
var levels = ["res://scenes/game.tscn","res://scenes/level2.tscn"]
|
||||
var timers = [10, 15]
|
||||
var timers = [20, 15]
|
||||
var currentLevel = 0
|
||||
#Signals from GC
|
||||
signal destroyBox(body)
|
||||
signal destroyBadguy(area)
|
||||
signal levelComplete (levelToLoad)
|
||||
signal playerHurt(amt)
|
||||
signal playerDeath
|
||||
|
||||
|
||||
signal tick(timeRemaining)
|
||||
signal coinsUpdate(coinsCollected, coinsAvailable)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
@@ -35,22 +39,40 @@ func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
func reset():
|
||||
totalCoinsCollected = 0
|
||||
player.health = player.starting_health
|
||||
countdown = timers[currentLevel]
|
||||
tick.emit.call_deferred(countdown)
|
||||
coinsUpdate.emit.call_deferred(totalCoinsCollected, totalCoins)
|
||||
playerHurt.emit.call_deferred(player.health)
|
||||
|
||||
func secondCounter():
|
||||
# print("one second")
|
||||
countdown -=1
|
||||
tick.emit(countdown)
|
||||
if countdown <=0:
|
||||
print("YOU LOSE")
|
||||
levelComplete.emit(levels[currentLevel])
|
||||
func coinCollected():
|
||||
totalCoinsCollected +=1
|
||||
coinsUpdate.emit(totalCoinsCollected, totalCoins)
|
||||
print("GC knows coin collected")
|
||||
|
||||
func playerDamaged():
|
||||
print("GC knows player taking damage")
|
||||
player.health -=20
|
||||
print("Health remaining: " +str(player.health))
|
||||
if player.health > 0:
|
||||
playerHurt.emit(player.health)
|
||||
else:
|
||||
playerDeath.emit()
|
||||
|
||||
func bulletHitBadguy(area):
|
||||
#decide what to do
|
||||
#how many bad guys reamining
|
||||
#send a signal
|
||||
destroyBadguy.emit(area)
|
||||
|
||||
func bulletHit(body):
|
||||
print("Game controller knows bullet hit something")
|
||||
if body.is_in_group("destructable"):
|
||||
@@ -73,3 +95,7 @@ func countCoins(value):
|
||||
func countBadGuys(value):
|
||||
totalBadGuys = value
|
||||
|
||||
|
||||
func playerDead():
|
||||
print("GC knows player is dead")
|
||||
levelComplete.emit(levels[currentLevel])
|
||||
@@ -2,6 +2,8 @@ extends Node
|
||||
@onready var crates: Node2D = $"../crates"
|
||||
@onready var coins: Node2D = $"../coins"
|
||||
@onready var enemies: Node2D = $"../enemies"
|
||||
@onready var player: CharacterBody2D = $"../CharacterBody2D"
|
||||
@onready var ui: Control = $"../CanvasLayer/ui"
|
||||
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
@@ -33,8 +35,19 @@ func _ready() -> void:
|
||||
Gamecontroller.countCoins(totalCoins)
|
||||
Gamecontroller.countCrates(totalCrates)
|
||||
|
||||
#Player interactions
|
||||
Gamecontroller.playerHurt.connect(player.hurtPlayer)
|
||||
Gamecontroller.playerDeath.connect(player.killPlayer)
|
||||
#Subscribe to vital events from GC
|
||||
Gamecontroller.levelComplete.connect(changeLevel)
|
||||
player.deathAnimationComplete.connect(Gamecontroller.playerDead)
|
||||
# ui events
|
||||
Gamecontroller.tick.connect(ui.timerUpdate)
|
||||
Gamecontroller.playerHurt.connect(ui.healthUpdate)
|
||||
Gamecontroller.playerDeath.connect(ui.healthGone)
|
||||
Gamecontroller.coinsUpdate.connect(ui.coinsUpdate)
|
||||
|
||||
Gamecontroller.destroyBadguy.connect(killBadguy)
|
||||
Gamecontroller.reset()
|
||||
|
||||
|
||||
@@ -50,6 +63,7 @@ func bulletFactory():
|
||||
if bulletArray.size() < 10:
|
||||
myBullet = bullet.instantiate()
|
||||
myBullet.connect("hit", onBulletHit)
|
||||
myBullet.connect("badguyHit", Gamecontroller.bulletHitBadguy)
|
||||
owner.add_child(myBullet)
|
||||
else:
|
||||
myBullet = bulletArray.pop_back()
|
||||
@@ -64,7 +78,9 @@ func makeBullet(position, speed):
|
||||
|
||||
return myBullet
|
||||
|
||||
|
||||
func killBadguy(area):
|
||||
area.queue_free()
|
||||
|
||||
func onBulletHit(bullet, body):
|
||||
bullet.position = Vector2(-100,-100)
|
||||
bullet.setSpeed(0)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
extends Control
|
||||
@onready var timer: Label = $VBoxContainer/HBoxContainer/MarginContainer3/timer
|
||||
@onready var health: Label = $VBoxContainer/HBoxContainer/MarginContainer/health
|
||||
@onready var coins: Label = $VBoxContainer/HBoxContainer/MarginContainer2/coins
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
func timerUpdate(timeRemaining):
|
||||
timer.text = str(timeRemaining)
|
||||
|
||||
func healthUpdate(healthRemaining):
|
||||
health.text = "HEALTH "+str(healthRemaining)
|
||||
func healthGone():
|
||||
health.text = "HEALTH 0"
|
||||
func coinsUpdate(coinsCollectedTotal, coinsAvailableTotal):
|
||||
coins.text = str(coinsCollectedTotal) + " of " +str(coinsAvailableTotal)
|
||||
Reference in new issue
Block a user