enemies, gamecontroller, playerdeath, coins
This commit is contained in:
1 parent
d84835dab2
commit
5ca3a193d1
21 files changed
+732
-91
No files matched your search
@@ -0,0 +1,40 @@
|
||||
class_name BadGuy extends Area2D
|
||||
|
||||
signal playerDamage
|
||||
var direction = 1
|
||||
const speed = 60
|
||||
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
||||
@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
|
||||
|
||||
|
||||
# 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,11 @@
|
||||
class_name Coin extends Area2D
|
||||
|
||||
signal coinCollected
|
||||
|
||||
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if body.is_in_group("player"):
|
||||
coinCollected.emit()
|
||||
queue_free()
|
||||
@@ -16,92 +16,119 @@ var pushTarget
|
||||
var pushLefEnabled = false
|
||||
var pushRightEnabled = false
|
||||
var isJumping = false
|
||||
|
||||
var animPlaying = "idle"
|
||||
var living = true
|
||||
|
||||
signal playerDead
|
||||
|
||||
@onready var right_target: Node2D = $RightTarget
|
||||
@onready var left_target: Node2D = $LeftTarget
|
||||
|
||||
func hurtPlayer(amt):
|
||||
print("Character knows it should be hurt")
|
||||
animPlaying = "hurt"
|
||||
playerSprite.play("Hurt")
|
||||
|
||||
func killPlayer():
|
||||
if living:
|
||||
living = false
|
||||
print("MORRIDO")
|
||||
animPlaying = "dead"
|
||||
playerSprite.play("Dead")
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
if living and not animPlaying =="hurt":
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# 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("ui_left", "ui_right")
|
||||
if Input.is_action_just_pressed("shove") && pushRightEnabled && faceLeft == false:
|
||||
print("Shove a box")
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSHFORCE * 2)
|
||||
pushRightEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shove") && faceLeft == true && pushLefEnabled == true:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSHFORCE * 2)
|
||||
pushLefEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("I'm an owl: Shoot shoot")
|
||||
var myBullet = %SceneManager.bulletFactory()
|
||||
if not faceLeft:
|
||||
myBullet.transform = right_target.global_transform
|
||||
myBullet.setSpeed(750)
|
||||
if faceLeft == true:
|
||||
myBullet.transform = left_target.global_transform
|
||||
myBullet.setSpeed(-750)
|
||||
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
if direction >0:
|
||||
faceLeft = false
|
||||
if direction <0:
|
||||
faceLeft = true
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
if faceLeft:
|
||||
playerSprite.flip_h = true
|
||||
else:
|
||||
playerSprite.flip_h = false
|
||||
|
||||
if is_on_floor():
|
||||
isJumping = false
|
||||
if direction == 0:
|
||||
playerSprite.play("Idle")
|
||||
else:
|
||||
playerSprite.play("Run")
|
||||
else:
|
||||
if not isJumping:
|
||||
playerSprite.play("Jump")
|
||||
isJumping = true
|
||||
|
||||
move_and_slide()
|
||||
if right_ray.is_colliding():
|
||||
print("Something is on my right")
|
||||
if not faceLeft:
|
||||
var collider = right_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("box"):
|
||||
print("This is a pushable box")
|
||||
pushTarget = collider
|
||||
pushRightEnabled = true
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
|
||||
if left_ray.is_colliding():
|
||||
print("Something is on my left")
|
||||
# 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("ui_left", "ui_right")
|
||||
if Input.is_action_just_pressed("shove") && pushRightEnabled && faceLeft == false:
|
||||
print("Shove a box")
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSHFORCE * 2)
|
||||
pushRightEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shove") && faceLeft == true && pushLefEnabled == true:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSHFORCE * 2)
|
||||
pushLefEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("I'm an owl: Shoot shoot")
|
||||
var myBullet = %SceneManager.bulletFactory()
|
||||
if not faceLeft:
|
||||
myBullet.transform = right_target.global_transform
|
||||
myBullet.setSpeed(750)
|
||||
if faceLeft == true:
|
||||
myBullet.transform = left_target.global_transform
|
||||
myBullet.setSpeed(-750)
|
||||
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
if direction >0:
|
||||
faceLeft = false
|
||||
if direction <0:
|
||||
faceLeft = true
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
if faceLeft:
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("box"):
|
||||
print("I can shove this box")
|
||||
pushTarget = collider
|
||||
pushLefEnabled = true
|
||||
else:
|
||||
pushLefEnabled = 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)
|
||||
playerSprite.flip_h = true
|
||||
else:
|
||||
playerSprite.flip_h = false
|
||||
|
||||
if is_on_floor():
|
||||
isJumping = false
|
||||
if direction == 0:
|
||||
playerSprite.play("Idle")
|
||||
else:
|
||||
playerSprite.play("Run")
|
||||
else:
|
||||
if not isJumping:
|
||||
playerSprite.play("Jump")
|
||||
isJumping = true
|
||||
|
||||
move_and_slide()
|
||||
if right_ray.is_colliding():
|
||||
print("Something is on my right")
|
||||
if not faceLeft:
|
||||
var collider = right_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("box"):
|
||||
print("This is a pushable box")
|
||||
pushTarget = collider
|
||||
pushRightEnabled = true
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
|
||||
if left_ray.is_colliding():
|
||||
print("Something is on my left")
|
||||
if faceLeft:
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("box"):
|
||||
print("I can shove this box")
|
||||
pushTarget = collider
|
||||
pushLefEnabled = true
|
||||
else:
|
||||
pushLefEnabled = 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 == "dead":
|
||||
### emit a custom signal
|
||||
playerDead.emit()
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
extends Node
|
||||
|
||||
var coinsCollected:int = 0
|
||||
var player:Resource
|
||||
|
||||
signal playerHurt()
|
||||
signal playerDeath()
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
player = load("res://scripts/res/dudestats.tres")
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func coinCollected():
|
||||
coinsCollected +=1
|
||||
print("Total coins collected "+str(coinsCollected) )
|
||||
|
||||
func playerDamage():
|
||||
if player.health > 0:
|
||||
player.health -= 20
|
||||
if player.health <=0:
|
||||
##kill him
|
||||
playerDeath.emit()
|
||||
else:
|
||||
##damage him
|
||||
playerHurt.emit(20)
|
||||
func reset():
|
||||
player.health = player.max_health
|
||||
coinsCollected = 0
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class_name CharacterStats extends Resource
|
||||
|
||||
|
||||
@export var max_health:int = 100
|
||||
@export var starting_heatlh: 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://bo060uupgs5s0"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/res/characterstats.gd" id="1_8xb6q"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_8xb6q")
|
||||
max_health = 100
|
||||
starting_heatlh = 100
|
||||
health = 100
|
||||
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://dqihgguptx2it"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/res/characterstats.gd" id="1_qhki2"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_qhki2")
|
||||
max_health = 50
|
||||
starting_heatlh = 50
|
||||
health = 50
|
||||
@@ -3,9 +3,32 @@ extends Node
|
||||
var bulletsFiredTotal := 0
|
||||
var bulletsMadeTotal := 0
|
||||
var bulletArray:Array = []
|
||||
@onready var coins: Node = $"../Coins"
|
||||
@onready var bad_guys: Node = $"../BadGuys"
|
||||
|
||||
@onready var player: CharacterBody2D = $"../CharacterBody2D"
|
||||
|
||||
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
|
||||
func _ready() -> void:
|
||||
### coins loop
|
||||
for n in coins.get_children():
|
||||
print("coin found")
|
||||
n.coinCollected.connect(GameController.coinCollected)
|
||||
### enemies loop
|
||||
for n in bad_guys.get_children():
|
||||
print("enemy found")
|
||||
n.playerDamage.connect(GameController.playerDamage)
|
||||
|
||||
### wire up GameController signals to Player
|
||||
GameController.playerHurt.connect(hurtPlayer)
|
||||
GameController.playerDeath.connect(killPlayer)
|
||||
|
||||
player.playerDead.connect(deadPlayer)
|
||||
|
||||
|
||||
func bulletFactory():
|
||||
print("Make a bullet at the factory")
|
||||
var mybullet
|
||||
@@ -21,11 +44,21 @@ func bulletFactory():
|
||||
bulletArray.push_front(mybullet)
|
||||
bulletsMadeTotal += 1
|
||||
return mybullet
|
||||
|
||||
|
||||
func onBulletHit(bullet, body):
|
||||
print("Scene Manager knows a bullet hit something")
|
||||
bullet.setSpeed(0)
|
||||
if body.is_in_group("shootable"):
|
||||
print("RIP")
|
||||
body.queue_free()
|
||||
|
||||
|
||||
func hurtPlayer(amt):
|
||||
player.hurtPlayer(amt)
|
||||
|
||||
func killPlayer():
|
||||
player.killPlayer()
|
||||
|
||||
func deadPlayer():
|
||||
GameController.reset()
|
||||
get_tree().reload_current_scene()
|
||||
|
||||
Reference in new issue
Block a user