player animations, variable triggers, working bullets
This commit is contained in:
1 parent
5dcc0e729b
commit
b52ead2557
22 files changed
+514
-70
No files matched your search
@@ -1,4 +1,5 @@
|
||||
extends CharacterBody2D
|
||||
class_name Player extends CharacterBody2D
|
||||
@onready var playerGraphic: AnimatedSprite2D = $AnimatedSprite2D
|
||||
|
||||
|
||||
const SPEED = 200.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,10 +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.
|
||||
@@ -47,11 +55,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,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_shape_entered(body_rid: RID, body: Node2D, body_shape_index: int, local_shape_index: int) -> 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
|
||||
@@ -9,17 +9,35 @@ var cratesDestroyed = 0
|
||||
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:
|
||||
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("win")
|
||||
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":
|
||||
#write code to powerup
|
||||
if body is Player:
|
||||
print("POWERRRRRR UP")
|
||||
#Player.SPEED = 700
|
||||
|
||||
|
||||
|
||||
func bulletHit(body):
|
||||
print("game controller knows bullet hit")
|
||||
if body is Crate:
|
||||
print("you've hit a crate")
|
||||
cratesDestroyed +=1
|
||||
%SceneManager.destroy(body)
|
||||
print("crates Remaining :: "+str(totalCrates - cratesDestroyed))
|
||||
|
||||
func numberOfCrates(value):
|
||||
totalCrates = value
|
||||
print("Game Controller know scrates "+str(totalCrates))
|
||||
@@ -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
|
||||
@@ -19,6 +22,7 @@ func bulletFactory():
|
||||
|
||||
bulletArray.push_front(mybullet)
|
||||
return mybullet
|
||||
|
||||
# order desk for bullets
|
||||
func makeBullet(position, speed):
|
||||
print("make a bullet")
|
||||
@@ -26,11 +30,30 @@ func makeBullet(position, speed):
|
||||
owner.add_child(newBullet)
|
||||
newBullet.setSpeed(speed)
|
||||
newBullet.transform = position
|
||||
|
||||
func bulletHit(bullet, body):
|
||||
print("tell the game controller a bullet hit smthn")
|
||||
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,11 +1,13 @@
|
||||
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 Active")
|
||||
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
@@ -13,4 +15,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