bullet factory, object pooling, state machine, animations, NPC
This commit is contained in:
1 parent
3bf7da43e3
commit
75d4918c45
18 files changed
+480
-25
No files matched your search
+30
-2
@@ -5,6 +5,10 @@ class_name SceneManager extends Node2D
|
||||
@onready var level: Node2D = $"../Level"
|
||||
@onready var game: GameController = $".."
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
var bulletArray:Array[Bullet] = []
|
||||
var totalAllowedBullets:int = 7
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
buildLevel()
|
||||
@@ -27,7 +31,7 @@ func buildLevel()->void:
|
||||
func updateTriggers()->void:
|
||||
if triggers:
|
||||
for obj in triggers.get_children():
|
||||
if obj is trigger:
|
||||
if obj is Trigger:
|
||||
if not obj.AreaTrigger.is_connected(game._on_trigger):
|
||||
obj.AreaTrigger.connect(game._on_trigger)
|
||||
func updateCrates()->void:
|
||||
@@ -41,6 +45,9 @@ func updateCrates()->void:
|
||||
totalCrates +=1
|
||||
game.crateUpdate(totalCrates)
|
||||
func destroy(body)->void:
|
||||
if body is Bullet:
|
||||
stashBullet(body)
|
||||
return
|
||||
body.queue_free()
|
||||
|
||||
func teleport(body)->void:
|
||||
@@ -53,6 +60,27 @@ func teleport(body)->void:
|
||||
|
||||
func loadLevel(level:String)->void:
|
||||
get_tree().call_deferred("change_scene_to_file", level)
|
||||
|
||||
func stashBullet(bullet:Bullet)->void:
|
||||
var stashPos:Vector2 = Vector2(-100, -100)
|
||||
bullet.position = stashPos
|
||||
bullet.setSpeed(0)
|
||||
bullet.set_process(false)
|
||||
func bullectFactory()->Bullet :
|
||||
var myBullet:Bullet
|
||||
#how many bullets have we made
|
||||
if bulletArray.size() <= totalAllowedBullets:
|
||||
myBullet = bullet.instantiate()
|
||||
if not myBullet.bulletDamageSignal.is_connected(game.bulletDamage):
|
||||
myBullet.bulletDamageSignal.connect(game.bulletDamage)
|
||||
game.add_child(myBullet)
|
||||
else:
|
||||
myBullet = bulletArray.pop_back()
|
||||
bulletArray.push_front(myBullet)
|
||||
return myBullet
|
||||
func makeBullet(targetPosition, speed)->void:
|
||||
print("make bullet")
|
||||
print("bullets created"+str(bulletArray.size()))
|
||||
var myBullet:Bullet = bullectFactory()
|
||||
myBullet.transform = targetPosition
|
||||
myBullet.setSpeed(speed)
|
||||
myBullet.set_process(true)
|
||||
+8
-2
@@ -1,11 +1,17 @@
|
||||
class_name bullet extends Area2D
|
||||
class_name Bullet extends Area2D
|
||||
|
||||
var speed:float = 700;
|
||||
signal bulletDamageSignal(body, bullet)
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
func setSpeed(value)->void:
|
||||
speed=value
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
bulletDamageSignal.emit(body, self)
|
||||
@@ -10,6 +10,7 @@ var timer := Timer.new()
|
||||
var timeAvailable := 10
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
get_window().grab_focus()
|
||||
add_child(timer)
|
||||
timer.wait_time = 1
|
||||
timer.one_shot = false
|
||||
@@ -32,10 +33,6 @@ func _on_trigger(body: Variant, effect, trigger) -> void:
|
||||
if body is Crate:
|
||||
match effect:
|
||||
"destroy":
|
||||
crateTotal -=1
|
||||
if crateTotal <=0:
|
||||
print("You WON!!!")
|
||||
levelChangeSignal.emit(currentScene)
|
||||
destroySignal.emit(body)
|
||||
"teleport":
|
||||
print("GC teleport")
|
||||
@@ -48,3 +45,11 @@ func _on_trigger(body: Variant, effect, trigger) -> void:
|
||||
func crateUpdate(cratesAmount)->void:
|
||||
crateTotal = cratesAmount
|
||||
print("GC updated crates: "+str(crateTotal))
|
||||
if crateTotal <=0:
|
||||
print("You WON!!!")
|
||||
levelChangeSignal.emit(currentScene)
|
||||
|
||||
func bulletDamage(body:Node2D, bullet:Bullet)->void:
|
||||
if body is Crate:
|
||||
destroySignal.emit(body)
|
||||
destroySignal.emit(bullet)
|
||||
@@ -0,0 +1,11 @@
|
||||
class_name NPCCharector extends CharacterBody2D
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
move_and_slide()
|
||||
func knockBack(direction, duration: float=0.2):
|
||||
velocity = direction
|
||||
await get_tree().create_timer(duration).timeout
|
||||
velocity = Vector2.ZERO
|
||||
@@ -0,0 +1 @@
|
||||
uid://cj5rps4p0rgja
|
||||
+74
-14
@@ -2,18 +2,46 @@ class_name Player extends CharacterBody2D
|
||||
|
||||
@onready var right_gun: Marker2D = $"Right Gun"
|
||||
@onready var left_gun: Marker2D = $"Left Gun"
|
||||
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
||||
|
||||
const SPEED = 300
|
||||
const JUMP_VELOCITY = -1000.0
|
||||
const BUMP_POWER = 50
|
||||
const JUMP_VELOCITY = -400.0
|
||||
const BUMP_POWER = 100000
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
var direction
|
||||
enum PlayerState{IDLE,RUNNING,JUMPING,FALLING}
|
||||
var current_player_state:PlayerState = PlayerState.IDLE
|
||||
var jumpUp:bool = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
#Handle Shoot
|
||||
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
handle_state()
|
||||
handle_animation()
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
|
||||
|
||||
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_input()->void:
|
||||
# Handle jump.
|
||||
direction = Input.get_axis("ui_left", "ui_right")
|
||||
if direction <0:
|
||||
player_graphic.flip_h = true
|
||||
if direction >0:
|
||||
player_graphic.flip_h = false
|
||||
if Input.is_action_just_pressed("ui_accept") :
|
||||
velocity.y = JUMP_VELOCITY
|
||||
current_player_state = PlayerState.JUMPING
|
||||
jumpUp = true
|
||||
if Input.is_action_just_pressed("Shoot"):
|
||||
print("player wants to shoot")
|
||||
match facing:
|
||||
@@ -23,13 +51,36 @@ func _physics_process(delta: float) -> void:
|
||||
FaceDirection.LEFT:
|
||||
print("shoot to the left")
|
||||
%SceneManager.makeBullet(left_gun.global_transform, -700)
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") :
|
||||
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")
|
||||
func handle_state()->void:
|
||||
match current_player_state:
|
||||
PlayerState.IDLE when velocity.x !=0:
|
||||
current_player_state = PlayerState.RUNNING
|
||||
PlayerState.RUNNING when velocity.x ==0:
|
||||
current_player_state = PlayerState.IDLE
|
||||
PlayerState.JUMPING when velocity.y > 0:
|
||||
current_player_state = PlayerState.FALLING
|
||||
PlayerState.FALLING when is_on_floor():
|
||||
if velocity.x ==0:
|
||||
current_player_state = PlayerState.IDLE
|
||||
if velocity.x !=0:
|
||||
current_player_state = PlayerState.RUNNING
|
||||
|
||||
func handle_animation()->void:
|
||||
match current_player_state:
|
||||
PlayerState.IDLE:
|
||||
player_graphic.play("idle")
|
||||
PlayerState.RUNNING:
|
||||
player_graphic.play("running")
|
||||
PlayerState.JUMPING:
|
||||
if jumpUp:
|
||||
player_graphic.play("jumping")
|
||||
PlayerState.FALLING:
|
||||
player_graphic.play("falling")
|
||||
|
||||
func handle_movement(delta)->void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
if direction >0:
|
||||
@@ -38,9 +89,18 @@ func _physics_process(delta: float) -> void:
|
||||
facing=FaceDirection.LEFT
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
func handle_collisions()->void:
|
||||
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_POWER)
|
||||
if c.get_collider() is NPCCharector:
|
||||
var myCharector:NPCCharector = c.get_collider()
|
||||
var direction = -c.get_normal()*BUMP_POWER
|
||||
myCharector.knockBack(direction)
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
match current_player_state:
|
||||
PlayerState.JUMPING:
|
||||
jumpUp = false
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
class_name trigger extends Area2D
|
||||
class_name Trigger extends Area2D
|
||||
|
||||
signal AreaTrigger(body, effect, trigger)
|
||||
@export var effect="destroy"
|
||||
|
||||
Reference in new issue
Block a user