enemies, gamecontroller, playerdeath, coins

This commit is contained in:
OddlyTimbot committed 2024-12-16 21:14:32 -05:00
1 parent 0df4295b7c
commit a5f092f125
21 files changed
+705 -92

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
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()
+12
View File
@@ -0,0 +1,12 @@
class_name Coin extends Area2D
signal coinCollected
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("player"):
coinCollected.emit()
queue_free()
+104 -80
View File
@@ -17,93 +17,117 @@ var pushLeftEnabled = 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
animPlaying = "death"
playerSprite.play(animPlaying)
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 && pushLeftEnabled == true:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSHFORCE *2 )
pushLeftEnabled = false
if Input.is_action_just_pressed("shoot"):
print("Shoot a bullet")
var myBullet = %SceneManager.bulletFactory()
if not faceLeft:
myBullet.transform = right_target.global_transform
myBullet.setSpeed(700)
if faceLeft ==true:
myBullet.transform = left_target.global_transform
myBullet.setSpeed(-700)
if direction:
velocity.x = direction * SPEED
if direction >0:
faceLeft = false
if direction < 0:
faceLeft = true
else:#TODO: implement jump
pass
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():
if faceLeft:
var collider = left_ray.get_collider()
if collider is Node:
if collider.is_in_group("box"):
pushTarget = collider
pushLeftEnabled = true
else:
pushLeftEnabled = false
# 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 && pushLeftEnabled == true:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSHFORCE *2 )
pushLeftEnabled = false
if Input.is_action_just_pressed("shoot"):
print("Shoot a bullet")
var myBullet = %SceneManager.bulletFactory()
if not faceLeft:
myBullet.transform = right_target.global_transform
myBullet.setSpeed(700)
if faceLeft ==true:
myBullet.transform = left_target.global_transform
myBullet.setSpeed(-700)
if direction:
velocity.x = direction * SPEED
if direction >0:
faceLeft = false
if direction < 0:
faceLeft = true
else:#TODO: implement jump
pass
velocity.x = move_toward(velocity.x, 0, SPEED)
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() * BUMPFORCE)
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():
if faceLeft:
var collider = left_ray.get_collider()
if collider is Node:
if collider.is_in_group("box"):
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() * BUMPFORCE)
func _on_animation_finished() -> void:
if animPlaying == "hurt":
animPlaying ="idle"
if animPlaying=="death":
## emit a custom signal
playerDead.emit()
+34
View File
@@ -0,0 +1,34 @@
extends Node
var coinsCollected:int = 0
var player:Resource
signal playerHurt(amt)
signal playerDeath()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
player = load("res://scripts/res/playerstats.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_health: int = 100
@export var health: int = 100
+9
View File
@@ -0,0 +1,9 @@
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://dlllgnb2qyb2p"]
[ext_resource type="Script" path="res://scripts/res/characterstats.gd" id="1_yv8hh"]
[resource]
script = ExtResource("1_yv8hh")
max_health = 50
starting_health = 50
health = 50
@@ -0,0 +1,9 @@
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://bqbgmfbankxvw"]
[ext_resource type="Script" path="res://scripts/res/characterstats.gd" id="1_8phuw"]
[resource]
script = ExtResource("1_8phuw")
max_health = 100
starting_health = 100
health = 100
+31
View File
@@ -3,9 +3,29 @@ extends Node
var bulletsFiredTotal :=0
var bulletsMadeTotal :=0
var bulletArray:Array = []
@onready var coins: Node = $"../coins"
@onready var enemies: Node = $"../enemies"
@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 enemies.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
@@ -28,3 +48,14 @@ func onBulletHit(bullet, body):
if body.is_in_group("shootable"):
print("explode this thing")
body.queue_free()
func hurtPlayer(amt):
player.hurtPlayer(amt)
func killPlayer():
player.killPlayer()
func deadPlayer():
GameController.reset()
get_tree().reload_current_scene()