bullet factory better, state machine for player, simple NPC
This commit is contained in:
1 parent
df3ebe62d2
commit
8d19d25a75
17 files changed
+474
-22
No files matched your search
@@ -2,6 +2,7 @@ class_name Bullet extends Area2D
|
||||
|
||||
var speed:float = 700
|
||||
|
||||
signal bulletDamageSignal(body: Node2D, bullet:Bullet)
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
@@ -10,3 +11,11 @@ func _ready() -> void:
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
position += transform.x * speed *delta
|
||||
|
||||
func setSpeed(value:float)->void:
|
||||
speed = value
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("Bullet hit something")
|
||||
bulletDamageSignal.emit(body, self)
|
||||
@@ -11,6 +11,7 @@ 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
|
||||
@@ -28,20 +29,17 @@ func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
#Signals from Triggers!
|
||||
func _on_trigger(body: Variant, effect, trigger) -> void:
|
||||
print("GC knows trigger...."+effect)
|
||||
#Crate triggers
|
||||
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 an object")
|
||||
teleportSignal.emit(body)
|
||||
|
||||
#Player triggers
|
||||
if body is Player:
|
||||
match effect:
|
||||
"powerup":
|
||||
@@ -51,4 +49,14 @@ 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:
|
||||
print("GC knows bullet hit")
|
||||
if body is Crate:
|
||||
#destroy the crate?
|
||||
destroySignal.emit(body)
|
||||
#destroy the bullet?
|
||||
destroySignal.emit(bullet)
|
||||
@@ -0,0 +1,22 @@
|
||||
class_name NPCCharacter extends CharacterBody2D
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
handle_movement(delta)
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_movement(_delta)->void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * _delta
|
||||
func knockBack(direction, duration:float=0.2):
|
||||
velocity = direction
|
||||
await get_tree().create_timer(duration).timeout
|
||||
velocity = Vector2.ZERO
|
||||
func handle_collisions()->void:
|
||||
for i in get_slide_collision_count():
|
||||
var collision = get_slide_collision(i)
|
||||
var collider = collision.get_collider()
|
||||
# Check the collider's group or type
|
||||
if collider is Player:
|
||||
print("NPC hit by player")
|
||||
@@ -0,0 +1 @@
|
||||
uid://gutmbv5ff4u4
|
||||
+58
-5
@@ -2,6 +2,7 @@ class_name Player extends CharacterBody2D
|
||||
|
||||
@onready var right_target: Marker2D = $RightTarget
|
||||
@onready var left_target: Marker2D = $LeftTarget
|
||||
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
@@ -9,11 +10,22 @@ const JUMP_VELOCITY = -400.0
|
||||
const BUMP_POWER = 50
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
#states for player
|
||||
enum PlayerState{IDLE,RUNNING,JUMPING, FALLING}
|
||||
var current_player_state:PlayerState = PlayerState.IDLE
|
||||
|
||||
var upJump:bool = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
handle_update_states()
|
||||
handle_update_animation()
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
#organize the game loop
|
||||
func handle_input()->void:
|
||||
# Handle Shoot
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("Player wants to shoot")
|
||||
@@ -28,6 +40,8 @@ func _physics_process(delta: float) -> void:
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
current_player_state = PlayerState.JUMPING
|
||||
upJump = true
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
@@ -36,13 +50,52 @@ func _physics_process(delta: float) -> void:
|
||||
velocity.x = direction * SPEED
|
||||
if direction <0:
|
||||
facing=FaceDirection.LEFT
|
||||
player_graphic.flip_h = true
|
||||
if direction >0:
|
||||
facing=FaceDirection.RIGHT
|
||||
player_graphic.flip_h = false
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
func handle_movement(_delta)->void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * _delta
|
||||
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 NPCCharacter:
|
||||
var direction = -c.get_normal()*BUMP_POWER
|
||||
c.get_collider().knockBack(direction)
|
||||
func handle_update_states()->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
|
||||
else:
|
||||
current_player_state = PlayerState.RUNNING
|
||||
func handle_update_animation()->void:
|
||||
match current_player_state:
|
||||
PlayerState.IDLE:
|
||||
player_graphic.play("idle")
|
||||
PlayerState.RUNNING:
|
||||
player_graphic.play("running")
|
||||
PlayerState.JUMPING:
|
||||
if upJump:
|
||||
player_graphic.play("jumping")
|
||||
PlayerState.FALLING:
|
||||
player_graphic.play("falling")
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
match current_player_state:
|
||||
PlayerState.JUMPING:
|
||||
upJump = false
|
||||
|
||||
@@ -4,6 +4,12 @@ class_name SceneManager extends Node2D
|
||||
@onready var level: Node2D = $"../Level"
|
||||
@onready var game: GameController = $".."
|
||||
|
||||
#preloads for dynamic instantiation
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
#array to keep track of bullets
|
||||
var bulletArray:Array[Bullet] = []
|
||||
#total number of bullets allowed to exist
|
||||
var totalAllowedBullets:int = 7
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
@@ -43,6 +49,9 @@ func updateCrates()->void:
|
||||
game.crateUpdate(totalCrates)
|
||||
|
||||
func destroy(body)->void:
|
||||
if body is Bullet:
|
||||
bulletStash(body)
|
||||
return
|
||||
body.queue_free()
|
||||
func teleport(body)->void:
|
||||
if body is Crate:
|
||||
@@ -54,5 +63,32 @@ func teleport(body)->void:
|
||||
|
||||
func loadLevel(level:String)->void:
|
||||
get_tree().call_deferred("change_scene_to_file", level)
|
||||
|
||||
func bulletStash(bullet:Bullet)->void:
|
||||
var stashPosition:Vector2 = Vector2(-100,-100)
|
||||
bullet.position = stashPosition
|
||||
bullet.setSpeed(0)
|
||||
bullet.set_process(false)
|
||||
|
||||
func bulletFactory()->Bullet:
|
||||
var myBullet:Bullet
|
||||
if bulletArray.size() <= totalAllowedBullets:
|
||||
#make a new bullet
|
||||
myBullet = bullet.instantiate()
|
||||
#connect to the game controller
|
||||
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 a bullet!")
|
||||
var myBullet:Bullet = bulletFactory()
|
||||
myBullet.transform = targetPosition
|
||||
myBullet.setSpeed(speed)
|
||||
myBullet.set_process(true)
|
||||
|
||||
Reference in new issue
Block a user