State machine, player anis, bullet pool and graphics
This commit is contained in:
1 parent
4e9a117b77
commit
ea9d513ff8
16 files changed
+480
-18
No files matched your search
@@ -16,3 +16,5 @@ func _process(delta: float) -> void:
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print ("Bullet hit something")
|
||||
bulletDamageSignal.emit(body,self)
|
||||
func setSpeed(value)->void:
|
||||
speed = value
|
||||
@@ -1,6 +1,9 @@
|
||||
class_name GameController extends Node2D
|
||||
|
||||
signal teleportSignal(body:RigidBody2D)
|
||||
signal destroySignal(Body)
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
@@ -16,7 +19,7 @@ func _on_trigger(effect: Variant, Object: Variant) -> void:
|
||||
match effect:
|
||||
"destroy":
|
||||
if not Object is Player:
|
||||
Object.queue_free()
|
||||
destroySignal.emit(Object)
|
||||
"teleport":
|
||||
print("Trigger wants to teleport")
|
||||
if Object is Crate :
|
||||
@@ -25,6 +28,6 @@ func _on_trigger(effect: Variant, Object: Variant) -> void:
|
||||
func _on_bullet_hit(body,bullet)-> void:
|
||||
print ("GC knows bullet hit")
|
||||
if body is Crate:
|
||||
body.queue_free()
|
||||
bullet.queue_free()
|
||||
destroySignal.emit(body)
|
||||
destroySignal.emit(bullet)
|
||||
|
||||
+72
-8
@@ -4,12 +4,19 @@ class_name Player extends CharacterBody2D
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Marker2D = $RightSpawn
|
||||
@onready var left_spawn: Marker2D = $LeftSpawn
|
||||
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
||||
|
||||
@export var shove_power:= 800
|
||||
@export var bump_power:= 100
|
||||
|
||||
var direction
|
||||
signal bulletSignal(pos,speed)
|
||||
|
||||
enum FaceDirection {LEFT,RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
enum PlayerStates {IDLE,RUNNING,JUMPING,FALLING}
|
||||
var current_player_state:PlayerStates = PlayerStates.IDLE
|
||||
var upJump:bool = false
|
||||
|
||||
var pushRightEnabled = false
|
||||
var pushLeftEnabled = false
|
||||
@@ -23,7 +30,33 @@ func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
handle_input()
|
||||
handle_movement()
|
||||
handle_states()
|
||||
handle_animations()
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
|
||||
|
||||
|
||||
move_and_slide()
|
||||
# find all things my player collided with
|
||||
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)
|
||||
|
||||
|
||||
func handle_input()->void:
|
||||
#Handle shoot
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("Player wants to shoot")
|
||||
#send a bullet signal
|
||||
if facing==FaceDirection.RIGHT:
|
||||
bulletSignal.emit(right_spawn.global_transform, 800)
|
||||
if facing==FaceDirection.LEFT:
|
||||
bulletSignal.emit(left_spawn.global_transform, -800)
|
||||
# Handle shove
|
||||
if Input.is_action_just_pressed("shove"):
|
||||
print("Player wants to shove")
|
||||
@@ -36,26 +69,25 @@ 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=PlayerStates.JUMPING
|
||||
upJump = true
|
||||
|
||||
# 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")
|
||||
direction = Input.get_axis("ui_left", "ui_right")
|
||||
func handle_movement()->void:
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
#Set player direction
|
||||
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()
|
||||
# find all things my player collided with
|
||||
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)
|
||||
func handle_collisions()->void:
|
||||
if right_cast.is_colliding():
|
||||
print("Something on my right")
|
||||
#get the thing I can shove
|
||||
@@ -74,3 +106,35 @@ func _physics_process(delta: float) -> void:
|
||||
pushTarget = collider
|
||||
else:
|
||||
pushLeftEnabled = false
|
||||
func handle_states()->void:
|
||||
match current_player_state:
|
||||
PlayerStates.IDLE when velocity.x !=0:
|
||||
current_player_state = PlayerStates.RUNNING
|
||||
PlayerStates.RUNNING when velocity.x ==0:
|
||||
current_player_state = PlayerStates.IDLE
|
||||
PlayerStates.JUMPING when velocity.y>0:
|
||||
#you falling
|
||||
current_player_state = PlayerStates.FALLING
|
||||
PlayerStates.FALLING when is_on_floor():
|
||||
if velocity.x ==0:
|
||||
current_player_state = PlayerStates.IDLE
|
||||
else:
|
||||
current_player_state = PlayerStates.RUNNING
|
||||
func handle_animations()->void:
|
||||
match current_player_state:
|
||||
PlayerStates.RUNNING:
|
||||
player_graphic.play("run")
|
||||
PlayerStates.IDLE:
|
||||
player_graphic.play("idle")
|
||||
PlayerStates.JUMPING:
|
||||
if upJump:
|
||||
player_graphic.play("jump")
|
||||
PlayerStates.FALLING:
|
||||
player_graphic.play("fall")
|
||||
|
||||
|
||||
func _on_player_animation_finished() -> void:
|
||||
match current_player_state:
|
||||
PlayerStates.JUMPING:
|
||||
upJump = false
|
||||
# Replace with function body.
|
||||
+46
-2
@@ -2,14 +2,22 @@ class_name SceneManager extends Node2D
|
||||
|
||||
@onready var triggers: Node2D = $"../Triggers"
|
||||
@onready var game: GameController = $".."
|
||||
@onready var bullet: Bullet = $"../Bullet"
|
||||
@onready var player: Player = $"../Player"
|
||||
|
||||
#recipe for bullets
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
var totalAllowedBullets:int =7
|
||||
|
||||
#collection
|
||||
var bulletArray:Array[Bullet] = []
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
buildLevel()
|
||||
game.teleportSignal.connect(teleport)
|
||||
bullet.bulletDamageSignal.connect(game._on_bullet_hit)
|
||||
game.destroySignal.connect(destroy)
|
||||
player.bulletSignal.connect(makeBullet)
|
||||
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
@@ -29,3 +37,39 @@ func teleport(body, _bodyposition=null)->void:
|
||||
var random_y = randf_range(0, viewport_size.y)
|
||||
var newPosition = Vector2(random_x, random_y)
|
||||
body.teleport_to(newPosition)
|
||||
#order desk (For bullets)
|
||||
func makeBullet(pos, speed)->void:
|
||||
#ask for a bullet from...the factory
|
||||
print("Make a bullet")
|
||||
var myBullet:Bullet = bulletFactory()
|
||||
myBullet.transform = pos
|
||||
myBullet.setSpeed(speed)
|
||||
myBullet.set_process(true)
|
||||
|
||||
func bulletFactory()->Bullet:
|
||||
var myBullet:Bullet
|
||||
#how many bullets have been created?
|
||||
if bulletArray.size() <= totalAllowedBullets:
|
||||
myBullet = bullet.instantiate()
|
||||
add_sibling(myBullet)
|
||||
#wire up a signal to the GC
|
||||
if not myBullet.bulletDamageSignal.is_connected(game._on_bullet_hit):
|
||||
myBullet.bulletDamageSignal.connect(game._on_bullet_hit)
|
||||
else:
|
||||
#recycle bulletxx
|
||||
myBullet = bulletArray.pop_back()
|
||||
#put the bullet in my collection
|
||||
bulletArray.push_front(myBullet)
|
||||
return myBullet
|
||||
func destroy(target)->void:
|
||||
if target is Bullet:
|
||||
stash(target)
|
||||
if target is Crate:
|
||||
target.queue_free()
|
||||
|
||||
func stash(target:Bullet)->void:
|
||||
print("Stash a bullet")
|
||||
var stashPosition:Vector2 = Vector2(-100,-100)
|
||||
target.position = stashPosition
|
||||
target.setSpeed(0)
|
||||
target.set_process(false)
|
||||
Reference in new issue
Block a user