added grenades and running animations|
This commit is contained in:
1 parent
321df5b9bd
commit
a245ebc135
14 files changed
+365
No files matched your search
@@ -0,0 +1,24 @@
|
||||
class_name Grenade extends RigidBody2D
|
||||
|
||||
var timer = Timer.new()
|
||||
|
||||
func _ready() -> void:
|
||||
add_child(timer)
|
||||
timer.wait_time = 2
|
||||
timer.one_shot = true
|
||||
timer.connect("timeout", explode)
|
||||
timer.start()
|
||||
|
||||
|
||||
func explode() -> void:
|
||||
print("explode")
|
||||
self.queue_free()
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func _on_body_entered(body: Node) -> void:
|
||||
print("collision with grenade")
|
||||
if body.is_in_group("destructable"):
|
||||
body.queue_free()
|
||||
explode()
|
||||
@@ -0,0 +1 @@
|
||||
uid://bwjaypkvgpb6t
|
||||
@@ -2,6 +2,9 @@ class_name Player extends CharacterBody2D
|
||||
|
||||
@onready var raycast_left: RayCast2D = $raycastLeft
|
||||
@onready var raycast_right: RayCast2D = $raycastRight
|
||||
@onready var left_marker: Marker2D = $left_marker
|
||||
@onready var right_marker: Marker2D = $right_marker
|
||||
@onready var graphic: AnimatedSprite2D = $graphic
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
@@ -9,10 +12,14 @@ var direction : float = 0
|
||||
enum FaceDirection { LEFT, RIGHT }
|
||||
var facing: FaceDirection = FaceDirection.LEFT
|
||||
var shove_target: RigidBody2D
|
||||
enum State { IDLE, RUNNING }
|
||||
var state: State = State.IDLE
|
||||
|
||||
func _physics_process(delta: float):
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
handle_state()
|
||||
handle_animation()
|
||||
move_and_slide() # pre-calc next position from velocity
|
||||
handle_collisions()
|
||||
|
||||
@@ -23,13 +30,22 @@ func handle_input():
|
||||
shove_target.apply_central_impulse(Vector2(shove_normal,0)*700)
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
if Input.is_action_just_pressed("chuck"):
|
||||
if facing == FaceDirection.LEFT:
|
||||
print("Chuck grenade left")
|
||||
%SceneManager.make_grenade(left_marker.global_transform, -1)
|
||||
else:
|
||||
print("Chuck grenade right")
|
||||
%SceneManager.make_grenade(right_marker.global_transform, 1)
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
direction = Input.get_axis("left", "right")
|
||||
if direction < 0:
|
||||
facing = FaceDirection.LEFT
|
||||
graphic.flip_h = true
|
||||
elif direction > 0:
|
||||
facing = FaceDirection.RIGHT
|
||||
graphic.flip_h = false
|
||||
|
||||
func handle_movement(delta: float):
|
||||
# Add the gravity.
|
||||
@@ -53,3 +69,17 @@ func handle_collisions():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
||||
|
||||
func handle_state():
|
||||
match state:
|
||||
State.IDLE when velocity.x != 0:
|
||||
state = State.RUNNING
|
||||
State.RUNNING when velocity.x == 0:
|
||||
state = State.IDLE
|
||||
|
||||
func handle_animation():
|
||||
match state:
|
||||
State.IDLE:
|
||||
graphic.play("idle")
|
||||
State.RUNNING:
|
||||
graphic.play("run")
|
||||
@@ -2,6 +2,7 @@ class_name SceneManager extends Node2D
|
||||
|
||||
@onready var game_node: Node2D = $".."
|
||||
@onready var crates: Node2D = $"../crates"
|
||||
var grenade = preload("res://scenes/grenade.tscn")
|
||||
|
||||
func _ready() -> void:
|
||||
print("scene manager is ready")
|
||||
@@ -14,6 +15,13 @@ func _process(delta: float) -> void:
|
||||
func build_level() -> void:
|
||||
update_crates()
|
||||
|
||||
func make_grenade(grenade_pos: Transform2D, grenade_dir: int) -> void:
|
||||
print("scene manager making grenade")
|
||||
var g:Grenade = grenade.instantiate()
|
||||
g.transform = grenade_pos
|
||||
owner.add_child(g)
|
||||
g.apply_central_impulse(Vector2(grenade_dir, 1)*400)
|
||||
|
||||
func update_crates() -> void:
|
||||
var crate_total:int = 0
|
||||
if crates:
|
||||
|
||||
Reference in new issue
Block a user