grenades, dynamic instantiation, state machine
This commit is contained in:
1 parent
7384736795
commit
a200dd1a51
12 files changed
+329
-3
No files matched your search
@@ -0,0 +1,24 @@
|
||||
class_name Grenade extends RigidBody2D
|
||||
|
||||
var timer = Timer.new()
|
||||
|
||||
func _ready() -> void:
|
||||
add_child(timer)
|
||||
#how long
|
||||
timer.wait_time = 2
|
||||
#one time only
|
||||
timer.one_shot = true
|
||||
timer.connect("timeout", explode)
|
||||
timer.start()
|
||||
|
||||
func explode()->void:
|
||||
print("ima splode")
|
||||
self.queue_free()
|
||||
|
||||
|
||||
|
||||
func _on_body_entered(body: Node) -> void:
|
||||
print("Collision w Grenade!!")
|
||||
if body.is_in_group("destructible"):
|
||||
body.queue_free()
|
||||
explode()
|
||||
@@ -0,0 +1 @@
|
||||
uid://bd8br3emm1nhb
|
||||
+31
-1
@@ -1,6 +1,9 @@
|
||||
class_name Player extends CharacterBody2D
|
||||
@onready var right_cast: RayCast2D = $RightCast
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Marker2D = $RightSpawn
|
||||
@onready var left_spawn: Marker2D = $LeftSpawn
|
||||
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
@@ -8,6 +11,8 @@ const JUMP_VELOCITY = -400.0
|
||||
var direction
|
||||
enum FaceDirection{LEFT,RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
enum PlayerState{IDLE,RUNNING}
|
||||
var current_player_state:PlayerState = PlayerState.IDLE
|
||||
|
||||
var pushTarget:RigidBody2D
|
||||
var pushEnabled:bool = false
|
||||
@@ -15,10 +20,16 @@ var pushEnabled:bool = false
|
||||
func _physics_process(delta: float) -> void:
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
handle_state()
|
||||
handle_animation()
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_input()->void:
|
||||
if Input.is_action_just_pressed("chuck"):
|
||||
print("Chuck a grenade")
|
||||
%SceneManager.makeGrenade(right_spawn.global_transform, 1)
|
||||
|
||||
if Input.is_action_just_pressed("shove") and pushEnabled:
|
||||
print("Shove somethin")
|
||||
# determine what direction to shove the thing
|
||||
@@ -38,8 +49,10 @@ func handle_input()->void:
|
||||
direction = Input.get_axis("left", "right")
|
||||
if direction<0:
|
||||
facing = FaceDirection.LEFT
|
||||
player_graphic.flip_h = true
|
||||
if direction>0:
|
||||
facing = FaceDirection.RIGHT
|
||||
player_graphic.flip_h = false
|
||||
|
||||
func handle_movement(delta)->void:
|
||||
# Add the gravity.
|
||||
@@ -72,4 +85,21 @@ func handle_collisions()->void:
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
#deliver the impact
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
||||
if not c.get_collider() is Grenade:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
||||
|
||||
func handle_state()->void:
|
||||
match current_player_state:
|
||||
PlayerState.IDLE when velocity.x !=0:
|
||||
#change to running
|
||||
current_player_state = PlayerState.RUNNING
|
||||
PlayerState.RUNNING when velocity.x ==0:
|
||||
current_player_state = PlayerState.IDLE
|
||||
|
||||
func handle_animation()->void:
|
||||
match current_player_state:
|
||||
PlayerState.IDLE:
|
||||
player_graphic.play("idle")
|
||||
PlayerState.RUNNING:
|
||||
player_graphic.play("run")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
class_name SceneManager extends Node2D
|
||||
@onready var game: Node2D = $".."
|
||||
@onready var crates: Node2D = $"../Crates"
|
||||
var grenade = preload("res://Scenes/grenade.tscn")
|
||||
|
||||
func _ready() -> void:
|
||||
print("Scene manager is ready!")
|
||||
@@ -25,3 +26,11 @@ func updateCrates()->void:
|
||||
_crateTotal +=1
|
||||
print("Number of crates: "+str(_crateTotal))
|
||||
game.totalCrates(_crateTotal)
|
||||
|
||||
func makeGrenade(_grenadePosition, _grenadeDirection)->void:
|
||||
print("SM wants to make a grenade")
|
||||
var myGrenade:Grenade = grenade.instantiate()
|
||||
owner.add_child(myGrenade)
|
||||
myGrenade.transform = _grenadePosition
|
||||
myGrenade.apply_central_impulse(Vector2(_grenadeDirection,-1)*200)
|
||||
|
||||
Reference in new issue
Block a user