Week 2: bullet factory, raycast, shove attack, unique names
This commit is contained in:
1 parent
435a143e59
commit
e3c11f0b4c
13 files changed
+240
-48
No files matched your search
@@ -0,0 +1 @@
|
||||
class_name Crate extends RigidBody2D
|
||||
@@ -0,0 +1 @@
|
||||
uid://d2pjirchmmx8w
|
||||
+67
-5
@@ -1,30 +1,92 @@
|
||||
extends CharacterBody2D
|
||||
class_name Player extends CharacterBody2D
|
||||
|
||||
@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 SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
|
||||
@export var pushpower = 80.0
|
||||
@export var bump_power = 60.0
|
||||
@export var shove_power = 600.0
|
||||
|
||||
enum FaceDirection {LEFT, RIGHT}
|
||||
var facing: FaceDirection = FaceDirection.LEFT
|
||||
|
||||
var pushTarget: RigidBody2D
|
||||
var pushEnable: bool = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle input
|
||||
handle_input()
|
||||
|
||||
# Move and handle player collisions
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_input() -> void:
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("Shoot")
|
||||
if facing == FaceDirection.LEFT:
|
||||
%SceneManager.makeBullet(left_spawn.global_transform, -700)
|
||||
if facing == FaceDirection.RIGHT:
|
||||
%SceneManager.makeBullet(right_spawn.global_transform, 700)
|
||||
|
||||
if Input.is_action_just_pressed("shove") and pushEnable:
|
||||
if facing == FaceDirection.LEFT:
|
||||
print("Shove attack LEFT!!!")
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * shove_power)
|
||||
pushEnable = false
|
||||
if facing == FaceDirection.RIGHT:
|
||||
print("Shove attack RIGHT!!!")
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * shove_power)
|
||||
pushEnable = false
|
||||
|
||||
# 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")
|
||||
var direction := Input.get_axis("ui_left", "ui_right")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
facing = FaceDirection.LEFT if direction < 0 else FaceDirection.RIGHT
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
func update_movement() -> void:
|
||||
pass
|
||||
|
||||
func update_states() -> void:
|
||||
pass
|
||||
|
||||
func handle_collisions() -> void:
|
||||
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() * pushpower)
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * bump_power)
|
||||
|
||||
if right_cast.is_colliding() and facing == FaceDirection.RIGHT:
|
||||
print("Right raycast hitting something")
|
||||
var collider = right_cast.get_collider()
|
||||
if collider is Node and collider is RigidBody2D:
|
||||
print("I think it's a crate")
|
||||
pushTarget = collider
|
||||
pushEnable = true
|
||||
|
||||
if left_cast.is_colliding() and facing == FaceDirection.LEFT:
|
||||
print("Left raycast hitting something")
|
||||
var collider = left_cast.get_collider()
|
||||
if collider is Node and collider is RigidBody2D:
|
||||
print("I think it's a crate")
|
||||
pushTarget = collider
|
||||
pushEnable = true
|
||||
|
||||
if not right_cast.is_colliding() and not left_cast.is_colliding():
|
||||
pushEnable = false
|
||||
@@ -0,0 +1,48 @@
|
||||
extends Node
|
||||
|
||||
@onready var game: GameController = $".."
|
||||
@onready var crates: Node2D = $"../Crates"
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
var bulletArray = []
|
||||
var totalAllowedBullets = 7
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
buildLevel()
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func buildLevel() -> void:
|
||||
# count crates
|
||||
var totalCrates = 0
|
||||
for obj in crates.get_children():
|
||||
if obj is Crate:
|
||||
totalCrates += 1
|
||||
# tell the game controller
|
||||
game.numberOfCrates(totalCrates)
|
||||
|
||||
|
||||
func bulletFactory():
|
||||
var mybullet
|
||||
if bulletArray.size() < totalAllowedBullets:
|
||||
#new bullet
|
||||
mybullet = bullet.instantiate()
|
||||
mybullet.connect("bulletHit", game.bulletDamage)
|
||||
owner.add_child(mybullet)
|
||||
else:
|
||||
#recycled bullet
|
||||
mybullet = bulletArray.pop_back()
|
||||
|
||||
bulletArray.push_front(mybullet)
|
||||
return mybullet
|
||||
|
||||
func makeBullet(position, speed):
|
||||
print("Scene manager make bullet")
|
||||
var mybullet = bulletFactory()
|
||||
mybullet.setSpeed(speed)
|
||||
mybullet.transform = position
|
||||
@@ -0,0 +1 @@
|
||||
uid://bijvpac45v6s1
|
||||
Reference in new issue
Block a user