player animations, variable triggers, working bullets
This commit is contained in:
1 parent
1ed49408f6
commit
452e3a402d
22 files changed
+511
-69
No files matched your search
@@ -1,4 +1,5 @@
|
||||
class_name Bullet extends Area2D
|
||||
@onready var bulletGraphic: Sprite2D = $Sprite2D
|
||||
|
||||
var speed = 750
|
||||
|
||||
@@ -6,6 +7,10 @@ signal hit(bullet, body)
|
||||
|
||||
func setSpeed(value):
|
||||
speed = value
|
||||
if speed <0:
|
||||
bulletGraphic.flip_h = true
|
||||
if speed >0:
|
||||
bulletGraphic.flip_h = false
|
||||
|
||||
#animation
|
||||
func _physics_process(delta: float) -> void:
|
||||
@@ -14,4 +19,5 @@ func _physics_process(delta: float) -> void:
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("bullet collision")
|
||||
hit.emit(self, body)
|
||||
if not body is Player:
|
||||
hit.emit(self, body)
|
||||
@@ -0,0 +1 @@
|
||||
class_name Crate extends RigidBody2D
|
||||
@@ -1,4 +1,4 @@
|
||||
extends Node2D
|
||||
class_name GameController extends Node2D
|
||||
|
||||
# Game Data
|
||||
var totalCrates = 4
|
||||
@@ -14,11 +14,26 @@ func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_trigger_area_trigger(body) -> void:
|
||||
print("Game controller knows about crate destruction")
|
||||
cratesDestroyed +=1
|
||||
totalCrates -=1
|
||||
# kill em
|
||||
body.queue_free()
|
||||
if totalCrates <=0:
|
||||
print("you won!!!")
|
||||
func _on_trigger_area_trigger(effect, body) -> void:
|
||||
print("Do effect "+effect)
|
||||
match effect:
|
||||
"destroy":
|
||||
#write code to destroy
|
||||
if body is Crate:
|
||||
%SceneManager.destroy(body)
|
||||
cratesDestroyed +=1
|
||||
"powerup":
|
||||
if body is Player:
|
||||
print("power up the player")
|
||||
|
||||
func bulletHit(body):
|
||||
print("Gamecontroller knows bullet hit")
|
||||
if body is Crate:
|
||||
print("You hit a crate")
|
||||
cratesDestroyed +=1
|
||||
%SceneManager.destroy(body)
|
||||
print("Crates Remaining :: "+str(totalCrates - cratesDestroyed))
|
||||
|
||||
func numberOfCrates(value):
|
||||
totalCrates = value
|
||||
print("Game Controller knows crates "+str(totalCrates) )
|
||||
@@ -1,4 +1,5 @@
|
||||
extends CharacterBody2D
|
||||
class_name Player extends CharacterBody2D
|
||||
@onready var playerGraphic: AnimatedSprite2D = $AnimatedSprite2D
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
@@ -9,6 +10,7 @@ var faceLeft = false
|
||||
var pushRightEnabled = false
|
||||
var pushLeftEnabled = false
|
||||
var pushTarget
|
||||
var isJumping = false
|
||||
|
||||
@onready var right_cast: RayCast2D = $rightCast
|
||||
@onready var left_cast: RayCast2D = $leftCast
|
||||
@@ -21,11 +23,16 @@ 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("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
isJumping = true
|
||||
playerGraphic.play("jump")
|
||||
|
||||
# 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")
|
||||
@@ -47,11 +54,19 @@ func _physics_process(delta: float) -> void:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
if direction:
|
||||
if not isJumping:
|
||||
playerGraphic.play("run")
|
||||
if direction ==0:
|
||||
if not isJumping:
|
||||
playerGraphic.play("idle")
|
||||
|
||||
if direction <0:
|
||||
faceLeft = true
|
||||
playerGraphic.flip_h = true
|
||||
if direction >0:
|
||||
faceLeft = false
|
||||
playerGraphic.flip_h = false
|
||||
|
||||
|
||||
move_and_slide()
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
extends Node
|
||||
@onready var gameController: GameController = $".."
|
||||
@onready var crates: Node2D = $"../crates"
|
||||
@onready var triggers: Node2D = $"../triggers"
|
||||
|
||||
var bulletsFiredTotal = 0
|
||||
var bulletsMadeTotal = 0
|
||||
@@ -30,3 +33,26 @@ func makeBullet(position, speed):
|
||||
|
||||
func bulletHit(bullet, body):
|
||||
print("Tell the game controller a bullet hit something")
|
||||
gameController.bulletHit(body)
|
||||
bullet.setSpeed(0)
|
||||
bullet.position = Vector2(-100,-100)
|
||||
|
||||
func destroy(body):
|
||||
body.queue_free()
|
||||
|
||||
func buildLevel():
|
||||
#how many crates?
|
||||
var totalCrates = 0
|
||||
for obj in crates.get_children():
|
||||
if obj is Crate:
|
||||
totalCrates +=1
|
||||
gameController.numberOfCrates(totalCrates)
|
||||
|
||||
#wire up the triggers
|
||||
for obj in triggers.get_children():
|
||||
if obj is Trigger:
|
||||
# wire up to GC
|
||||
obj.areaTrigger.connect(gameController._on_trigger_area_trigger)
|
||||
|
||||
func _ready() -> void:
|
||||
buildLevel()
|
||||
@@ -1,6 +1,7 @@
|
||||
extends Area2D
|
||||
class_name Trigger extends Area2D
|
||||
@export var effect="alert"
|
||||
|
||||
signal areaTrigger(Object)
|
||||
signal areaTrigger(effect, Object)
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
print("Trigger is here")
|
||||
@@ -13,4 +14,4 @@ func _process(delta: float) -> void:
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("Body collision in trigger")
|
||||
areaTrigger.emit(body)
|
||||
areaTrigger.emit(effect, body)
|
||||
Reference in new issue
Block a user