tiles, shooting, playergraphic/state machine
This commit is contained in:
1 parent
616afe6dbb
commit
3521f229d7
22 files changed
+992
-15
No files matched your search
+1
-1
@@ -1,13 +1,13 @@
|
||||
class_name Bullet extends Area2D
|
||||
var speed:float = 700
|
||||
signal bulletHitSignal(body, bullet)
|
||||
|
||||
func setSpeed(value)->void:
|
||||
speed = value
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if body is Crate:
|
||||
bulletHitSignal.emit(body,self)
|
||||
@@ -40,6 +40,9 @@ func crateTotal(howMany):
|
||||
if numberOfCrates == 0:
|
||||
print("YOU WON!!!")
|
||||
get_tree().reload_current_scene()
|
||||
|
||||
func bulletDamage(body, _bullet)->void:
|
||||
if body.is_in_group("shootable"):
|
||||
destroySignal.emit(body)
|
||||
destroySignal.emit(_bullet)
|
||||
|
||||
+72
-6
@@ -8,6 +8,7 @@ const JUMP_VELOCITY = -400.0
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Marker2D = $RightSpawn
|
||||
@onready var left_spawn: Marker2D = $LeftSpawn
|
||||
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
||||
|
||||
var pushRightEnabled = false
|
||||
var pushLeftEnabled = false
|
||||
@@ -17,18 +18,33 @@ var pushTimer = 0
|
||||
enum FaceDirection {RIGHT,LEFT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
enum playerStates {IDLE, RUNNING, JUMPING, FALLING, CHARGING, SHOVING}
|
||||
var current_player_state:playerStates = playerStates.IDLE
|
||||
var upJump = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
handle_movement(delta)
|
||||
handle_input()
|
||||
handle_states()
|
||||
handle_animation()
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_input()->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
|
||||
#Handle shove target
|
||||
if Input.is_action_just_pressed("shove"):
|
||||
print("I want to shove")
|
||||
pushTimer = Time.get_ticks_msec()
|
||||
if pushRightEnabled && facing==FaceDirection.RIGHT:
|
||||
current_player_state = playerStates.CHARGING
|
||||
pushTimer = Time.get_ticks_msec()
|
||||
if pushLeftEnabled && facing==FaceDirection.LEFT:
|
||||
current_player_state = playerStates.CHARGING
|
||||
pushTimer = Time.get_ticks_msec()
|
||||
if Input.is_action_just_released("shove"):
|
||||
var pushForce = Time.get_ticks_msec() - pushTimer
|
||||
if pushForce>4000:
|
||||
@@ -36,8 +52,10 @@ func _physics_process(delta: float) -> void:
|
||||
if pushRightEnabled and facing==FaceDirection.RIGHT:
|
||||
#deliver the boom
|
||||
pushSignal.emit(pushTarget, Vector2(1,0)*100*(pushForce/100))
|
||||
current_player_state = playerStates.SHOVING
|
||||
if pushLeftEnabled and facing==FaceDirection.LEFT:
|
||||
pushSignal.emit(pushTarget, Vector2(-1,0)*100*(pushForce/100))
|
||||
current_player_state = playerStates.SHOVING
|
||||
|
||||
#Handle Shoot Bullet
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
@@ -48,6 +66,7 @@ func _physics_process(delta: float) -> void:
|
||||
shootSignal.emit(right_spawn.global_transform, 800)
|
||||
FaceDirection.LEFT:
|
||||
shootSignal.emit(left_spawn.global_transform, -800)
|
||||
|
||||
# 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")
|
||||
@@ -55,12 +74,19 @@ func _physics_process(delta: float) -> void:
|
||||
velocity.x = direction * SPEED
|
||||
if direction >0:
|
||||
facing = FaceDirection.RIGHT
|
||||
player_graphic.flip_h = false
|
||||
else:
|
||||
facing = FaceDirection.LEFT
|
||||
player_graphic.flip_h = true
|
||||
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:
|
||||
if right_cast.is_colliding():
|
||||
print("Right raycast collision")
|
||||
var collider = right_cast.get_collider()
|
||||
@@ -84,3 +110,43 @@ func _physics_process(delta: float) -> void:
|
||||
#is this bonkable? reverse normal
|
||||
if c.get_collider() is RigidBody2D:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
||||
|
||||
# Exciting new stuff
|
||||
func handle_states()->void:
|
||||
match current_player_state:
|
||||
playerStates.IDLE when velocity.x != 0:
|
||||
print("I run")
|
||||
current_player_state = playerStates.RUNNING
|
||||
playerStates.RUNNING when velocity.x == 0:
|
||||
current_player_state = playerStates.IDLE
|
||||
playerStates.JUMPING when velocity.y>0:
|
||||
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_animation()->void:
|
||||
match current_player_state:
|
||||
playerStates.RUNNING:
|
||||
player_graphic.play("running")
|
||||
playerStates.IDLE:
|
||||
player_graphic.play("idle")
|
||||
playerStates.JUMPING:
|
||||
if upJump:
|
||||
player_graphic.play("jumping")
|
||||
playerStates.FALLING:
|
||||
player_graphic.play("falling")
|
||||
playerStates.CHARGING:
|
||||
player_graphic.play("charging")
|
||||
playerStates.SHOVING:
|
||||
player_graphic.play("shoving")
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
match current_player_state:
|
||||
playerStates.JUMPING:
|
||||
upJump = false
|
||||
playerStates.SHOVING:
|
||||
current_player_state = playerStates.IDLE
|
||||
@@ -37,8 +37,23 @@ func updateCrates()->void:
|
||||
func pushTarget(body,pushDirection)->void:
|
||||
if body is RigidBody2D:
|
||||
body.apply_central_impulse(pushDirection)
|
||||
|
||||
func stashBullet(bullet:Bullet)->void:
|
||||
# hide bullet
|
||||
var stashPosition:Vector2 = Vector2(-100,-100)
|
||||
bullet.position = stashPosition
|
||||
bullet.setSpeed(0)
|
||||
bullet.set_process(false)
|
||||
|
||||
func destroy(body)->void:
|
||||
body.queue_free()
|
||||
match body:
|
||||
var bod when bod is Bullet:
|
||||
stashBullet(body)
|
||||
var bod when bod is Crate:
|
||||
body.queue_free()
|
||||
_:
|
||||
body.queue_free()
|
||||
|
||||
func bulletFactory()->Bullet:
|
||||
var myBullet:Bullet
|
||||
#how many bullets have been produced?
|
||||
@@ -49,12 +64,15 @@ func bulletFactory()->Bullet:
|
||||
if not myBullet.bulletHitSignal.is_connected(game.bulletDamage):
|
||||
myBullet.bulletHitSignal.connect(game.bulletDamage)
|
||||
else:
|
||||
# gets oldest one - pop_back
|
||||
myBullet = bulletArray.pop_back()
|
||||
|
||||
bulletArray.push_front(myBullet)
|
||||
return myBullet
|
||||
|
||||
func makeBullet(spawnPosition, speed)->void:
|
||||
print("make bullet")
|
||||
var myBullet:Bullet = bulletFactory()
|
||||
myBullet.transform = spawnPosition
|
||||
myBullet.setSpeed(speed)
|
||||
myBullet.set_process(true)
|
||||
Reference in new issue
Block a user