week 2: force push and bullets!
This commit is contained in:
1 parent
3e8d5754f9
commit
0968ee75ae
13 files changed
+207
-45
No files matched your search
@@ -0,0 +1,16 @@
|
||||
class_name Bullet extends Area2D
|
||||
|
||||
signal bulletHitSignal(body, bullet)
|
||||
|
||||
var SPEED: float = 700.0
|
||||
|
||||
func setSpeed(new_speed: float):
|
||||
SPEED = new_speed
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
position += transform.x * SPEED * delta
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if body.is_in_group("shootable"):
|
||||
bulletHitSignal.emit(body, self)
|
||||
@@ -0,0 +1 @@
|
||||
uid://do4exob6g0cfd
|
||||
@@ -0,0 +1 @@
|
||||
class_name Crate extends RigidBody2D
|
||||
@@ -0,0 +1 @@
|
||||
uid://boay68qas1gpa
|
||||
@@ -1,7 +1,5 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
@@ -17,3 +15,6 @@ func _on_trigger(effect: Variant, body: Variant) -> void:
|
||||
if not body is Player:
|
||||
body.queue_free()
|
||||
|
||||
func onBulletHitSignal(body: Node, bullet: Bullet):
|
||||
body.queue_free()
|
||||
|
||||
+71
-13
@@ -1,31 +1,89 @@
|
||||
class_name Player extends CharacterBody2D
|
||||
|
||||
@onready var scene_manager: SceneManager = %SceneManager
|
||||
@onready var right_cast: RayCast2D = $RightCast
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Node2D = $RightSpawn
|
||||
@onready var left_spawn: Node2D = $LeftSpawn
|
||||
|
||||
|
||||
const SHOVE_STRENGTH = 700
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
|
||||
var direction: float
|
||||
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing: FaceDirection = FaceDirection.RIGHT
|
||||
|
||||
var pushTarget
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# 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")
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
|
||||
func handle_movement(delta: float):
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
func handle_input():
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# 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("ui_left", "ui_right")
|
||||
|
||||
if direction > 0:
|
||||
facing = FaceDirection.RIGHT
|
||||
elif direction < 0:
|
||||
facing = FaceDirection.LEFT
|
||||
|
||||
var is_shoving = Input.is_action_just_pressed("shove")
|
||||
if is_shoving and pushTarget is RigidBody2D:
|
||||
var shoveDirection: int
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
shoveDirection = 1
|
||||
FaceDirection.LEFT:
|
||||
shoveDirection = -1
|
||||
pushTarget.apply_central_impulse(Vector2i(shoveDirection, 0)*SHOVE_STRENGTH)
|
||||
|
||||
var is_shooting = Input.is_action_just_pressed("shoot")
|
||||
if is_shooting:
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
scene_manager.make_bullet(right_spawn.global_transform, SPEED)
|
||||
FaceDirection.LEFT:
|
||||
scene_manager.make_bullet(left_spawn.global_transform, -1*SPEED)
|
||||
|
||||
func handle_collisions():
|
||||
for i in get_slide_collision_count():
|
||||
var collision := get_slide_collision(i)
|
||||
var collider := collision.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
collider.apply_central_impulse(-collision.get_normal()*100)
|
||||
|
||||
if right_cast.is_colliding() && facing == FaceDirection.RIGHT:
|
||||
collider = right_cast.get_collider()
|
||||
if collider is Node and collider.is_in_group("shovable"):
|
||||
pushTarget = collider
|
||||
|
||||
elif left_cast.is_colliding() && facing == FaceDirection.LEFT:
|
||||
collider = left_cast.get_collider()
|
||||
if collider is Node and collider.is_in_group("shovable"):
|
||||
pushTarget = collider
|
||||
else:
|
||||
pushTarget = null
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
class_name SceneManager extends Node2D
|
||||
|
||||
const bullet_scene = preload("res://scenes/bullet.tscn")
|
||||
|
||||
@onready var game: Node2D = $".."
|
||||
|
||||
var MAX_BULLET := 50
|
||||
var available_bullets = []
|
||||
|
||||
func _ready() -> void:
|
||||
for i in range(MAX_BULLET):
|
||||
var new_bullet = bullet_scene.instantiate()
|
||||
new_bullet.bulletHitSignal.connect(game.onBulletHitSignal)
|
||||
available_bullets.push_front(new_bullet)
|
||||
|
||||
func make_bullet(start_position: Transform2D, speed: int):
|
||||
var new_bullet = bullet_factory(start_position, speed)
|
||||
if new_bullet:
|
||||
owner.add_child(new_bullet)
|
||||
|
||||
func reload_bullet(bullet: Bullet):
|
||||
available_bullets.push(bullet)
|
||||
|
||||
func bullet_factory(start_position: Transform2D, speed: int) -> Bullet:
|
||||
var shot_bullet = available_bullets.pop_back()
|
||||
shot_bullet.global_transform = start_position
|
||||
shot_bullet.setSpeed(speed)
|
||||
|
||||
available_bullets.push_front(shot_bullet)
|
||||
|
||||
return shot_bullet
|
||||
@@ -0,0 +1 @@
|
||||
uid://vi7u52xttkpb
|
||||
Reference in new issue
Block a user