enemies, game controller, playerdeath, coins, player hurt
This commit is contained in:
1 parent
082386ee29
commit
8966ac2165
21 files changed
+718
-88
No files matched your search
@@ -0,0 +1,41 @@
|
||||
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()
|
||||
@@ -17,97 +17,123 @@ var pushTarget
|
||||
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 hurt")
|
||||
animPlaying = "hurt"
|
||||
playerSprite.play("hurt")
|
||||
|
||||
func killPlayer():
|
||||
print("Characters know it is the end")
|
||||
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) * PUSH_FORCE * 2)
|
||||
pushRightEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shove") && faceLeft == true && pushLeftEnabled == true:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 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(400)
|
||||
if faceLeft:
|
||||
mybullet.transform = left_target.global_transform
|
||||
mybullet.setSpeed(-400)
|
||||
|
||||
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
|
||||
# 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) * PUSH_FORCE * 2)
|
||||
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("This is a pushable box")
|
||||
pushTarget = collider
|
||||
pushLeftEnabled = true
|
||||
else:
|
||||
pushLeftEnabled = false
|
||||
if Input.is_action_just_pressed("shove") && faceLeft == true && pushLeftEnabled == true:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
|
||||
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)
|
||||
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(400)
|
||||
if faceLeft:
|
||||
mybullet.transform = left_target.global_transform
|
||||
mybullet.setSpeed(-400)
|
||||
|
||||
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")
|
||||
if faceLeft:
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("box"):
|
||||
print("This is a pushable 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() * BUMP_FORCE)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
if animPlaying == "hurt":
|
||||
animPlaying = "idle"
|
||||
if animPlaying == "death":
|
||||
## emita custom signal
|
||||
playerDead.emit()
|
||||
@@ -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 collecte " + 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
|
||||
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://cuqtpu3cm6xgo"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/res/characterstats.gd" id="1_in2m2"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_in2m2")
|
||||
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://d26och4kddhoe"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/res/characterstats.gd" id="1_yty15"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_yty15")
|
||||
max_health = 100
|
||||
starting_health = 100
|
||||
health = 100
|
||||
@@ -3,9 +3,30 @@ 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)
|
||||
## enemy loop
|
||||
for n in enemies.get_children():
|
||||
print("enemy found")
|
||||
n.playerDamage.connect(GameController.playerDamage)
|
||||
|
||||
## wire up game controller 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
|
||||
@@ -29,3 +50,13 @@ 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()
|
||||
Reference in new issue
Block a user