raycasts, melee attack,factory for bullets , spawn points, dynamic instatiation

This commit is contained in:
JayisonDevadas committed 2025-08-11 21:08:57 -04:00
1 parent c8a99da3e1
commit 309f5f9941
11 files changed
+210 -48

No files matched your search

+18
View File
@@ -0,0 +1,18 @@
class_name Bullet extends Area2D
var speed:float = 700
signal bulletDamageSignal(body,bullet)
func setSpeed(value:float):
speed = value
func _physics_process(delta: float) -> void:
position += transform.x * speed * delta
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("shootables"):
bulletDamageSignal.emit(body,self)
+1
View File
@@ -0,0 +1 @@
class_name Crate extends RigidBody2D
+5
View File
@@ -15,3 +15,8 @@ func _on_trigger(effect: Variant, body:Variant) -> void:
print("GC sees trigger "+effect)
if not body is Player:
body.queue_free()
func bulletDamage(body, bullet):
print("gc knows about bullet hit")
body.queue_free()
#apply_central_impulse(Vector2(shoveDirection,0)*700)
+65 -12
View File
@@ -1,29 +1,82 @@
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
var direction
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection = FaceDirection.RIGHT
func _physics_process(delta):
var pushTarget
var pushEnabled := false
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) -> void:
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if not is_on_floor():
velocity += get_gravity() * delta
move_and_slide()
func handle_input():
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
direction = Input.get_axis("ui_left", "ui_right")
if direction <0:
facing =FaceDirection.LEFT
if direction > 0:
facing = FaceDirection.RIGHT
if Input.is_action_just_pressed("shove") && pushEnabled:
var shoveDirection:int
match facing:
FaceDirection.RIGHT:
#this is where tabbing does'nt work
shoveDirection = 1
FaceDirection.LEFT:
shoveDirection = -1
pushTarget.apply_central_impulse(Vector2(shoveDirection,0)*700)
if Input.is_action_just_pressed("shoot"):
match facing:
FaceDirection.RIGHT:
%SceneManager.makeBullet(right_spawn.global_transform,700)
FaceDirection.LEFT:
%SceneManager.makeBullet(left_spawn.global_transform,-700)
func handle_collisions():
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() * 100)
# i?
if right_cast.is_colliding() && facing == FaceDirection.RIGHT:
var collider = right_cast.get_collider()
if collider is Node && collider is RigidBody2D && collider.is_in_group("pushables"):
pushTarget = collider
pushEnabled = true
if left_cast.is_colliding() && facing == FaceDirection.LEFT:
var collider = left_cast.get_collider()
if collider is Node && collider is RigidBody2D && collider.is_in_group("pushables"):
pushTarget = collider
pushEnabled = true
if not right_cast.is_colliding() && not left_cast.is_colliding():
pushEnabled = false
+30
View File
@@ -0,0 +1,30 @@
class_name SceneManager extends Node2D
var bulletArray = []
var totalAllowedBullets = 7
var bullet = preload("res://scenes/bullet.tscn")
@onready var game: Node2D = $".."
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
# makes and recycles bullets
func bulletFactory():
var myBullet:Bullet
if bulletArray.size() < totalAllowedBullets:
myBullet = bullet.instantiate()
myBullet.bulletDamageSignal.connect(game.bulletDamage)
owner.add_child(myBullet)
else:
myBullet = bulletArray.pop_back()
bulletArray.push_front(myBullet)
return myBullet
func makeBullet(position, speed):
var someBullet = bulletFactory()
someBullet.setSpeed(speed)
someBullet.transform = position