added shove, raycasts, bullet factory, bullets and scene manager

This commit is contained in:
GavinBall committed 2025-04-14 20:58:05 -04:00
1 parent e6c049af5c
commit bdb5b5f1fd
13 files changed
+244 -93

No files matched your search

+18
View File
@@ -0,0 +1,18 @@
class_name Bullet extends Area2D
@export var speed = 700.0
@export var damage = 10.0
signal bullet_hit(body, bullet)
func set_speed(value):
speed = value
func _physics_process(delta: float) -> void:
position += transform.x * speed * delta
func _on_body_entered(body: Node2D) -> void:
if not body is Player:
print('bullet hit')
bullet_hit.emit(body, self)
+1
View File
@@ -0,0 +1 @@
uid://cwj2jevwb1w6d
+1
View File
@@ -0,0 +1 @@
class_name Crate extends RigidBody2D
+1
View File
@@ -0,0 +1 @@
uid://dknsbg2xfjd1q
+15 -7
View File
@@ -25,10 +25,18 @@ func _process(delta: float) -> void:
func _on_trigger_trigger_fired(effect: Variant, body: Variant) -> void:
print('Game Controller knows :: ' + effect)
if effect =='Destroy':
total_crates -= 1
print('Crates Remaining ' + str(total_crates))
body.queue_free()
if total_crates == 0:
print('Winner!')
get_tree().reload_current_scene()
if body is RigidBody2D:
if effect =='Destroy':
total_crates -= 1
print('Crates Remaining ' + str(total_crates))
body.queue_free()
if total_crates == 0:
print('Winner!')
get_tree().reload_current_scene()
func number_of_crates(value):
total_crates = value
print('total crates at GC: ' + str(total_crates))
func bullet_damage(body, bullet):
print('Game Controller knows about bullet hit')
+74 -7
View File
@@ -1,20 +1,57 @@
extends CharacterBody2D
class_name Player extends CharacterBody2D
# raycasts
@onready var right_cast: RayCast2D = $RightCast
@onready var left_cast: RayCast2D = $LeftCast
# spawn points
@onready var right_spawn: Node2D = $RightSpawn
@onready var left_spawn: Node2D = $LeftSpawn
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@export var push_power = 80.0
@export var bump_power = 50.0
@export var shove_power = 500.0
@export var lift_power = -0.3
enum FaceDirection{LEFT, RIGHT}
var facing : FaceDirection = FaceDirection.RIGHT
var push_target:RigidBody2D
var push_enabled:bool = false
func _physics_process(delta: float) -> void:
# Add the gravity.
if is_on_ceiling():
pass
if not is_on_floor():
velocity += get_gravity() * delta
handle_input()
move_and_slide()
handle_collisions()
func handle_input() -> void:
if Input.is_action_just_pressed('shoot'):
print('Shoot a bullet')
if facing == FaceDirection.LEFT:
%SceneManager.make_bullet(left_spawn.global_transform, -700)
if facing == FaceDirection.RIGHT:
%SceneManager.make_bullet(right_spawn.global_transform, 700)
if Input.is_action_just_pressed("shove") && push_enabled:
print('Shove off!')
if facing == FaceDirection.LEFT:
print('facing left')
push_target.apply_central_impulse(Vector2(-1,lift_power) * shove_power)
push_enabled = false
if facing == FaceDirection.RIGHT:
print('facing right')
push_target.apply_central_impulse(Vector2(1,lift_power) * shove_power)
push_enabled = false
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
@@ -24,11 +61,41 @@ func _physics_process(delta: float) -> void:
var direction := Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
if direction < 0:
facing = FaceDirection.LEFT
if direction > 0:
facing = FaceDirection.RIGHT
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
func update_movement() -> void:
pass
func update_states() -> void:
pass
move_and_slide()
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() * push_power)
c.get_collider().apply_central_impulse(-c.get_normal() * bump_power)
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
print('right raycast is hitting something')
var collider = right_cast.get_collider()
if collider is Node && collider is RigidBody2D:
print('I think it is a crate')
push_target = collider
push_enabled = true
if left_cast.is_colliding() && facing == FaceDirection.LEFT:
print('left raycast is hitting something')
var collider = left_cast.get_collider()
if collider is Node && collider is RigidBody2D:
print('I think its a crate!')
push_target = collider
push_enabled = true
if not right_cast.is_colliding() and not left_cast.is_colliding():
push_enabled = false
+40
View File
@@ -0,0 +1,40 @@
extends Node
@onready var game: GameController = $".."
@onready var crates: Node2D = $"../Crates"
var bullet = preload("res://scenes/bullet.tscn")
var bullet_array = []
var total_allowed_bullets = 7
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
build_level()
func build_level() -> void:
# count crates
var total_crates = 0
for obj in crates.get_children():
if obj is Crate:
total_crates += 1
# tell game controller
game.number_of_crates(total_crates)
func bullet_factory():
var my_bullet
if bullet_array.size() < total_allowed_bullets:
# new bullet
my_bullet = bullet.instantiate()
my_bullet.connect('bullet_hit', game.bullet_damage)
owner.add_child(my_bullet)
else:
# recycle bullet
my_bullet = bullet_array.pop_back()
bullet_array.push_front(my_bullet)
return my_bullet
func make_bullet(position, speed):
print('Scene manager makes a bullet')
var my_bullet = bullet_factory()
my_bullet.set_speed(speed)
my_bullet.transform = position
+1
View File
@@ -0,0 +1 @@
uid://7iv62cso22ox