bullet factory, push attack

This commit is contained in:
AliJaffar committed 2025-03-03 20:40:24 -05:00
1 parent b35e6f4f60
commit 5dcc0e729b
7 files changed
+256 -79

No files matched your search

+59 -3
View File
@@ -1,8 +1,20 @@
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -420.0
const SPEED = 200.0
const JUMP_VELOCITY = -450.0
const PUSH_FORCE = 500
var faceLeft = false
var pushRightEnabled = false
var pushLeftEnabled = false
var pushTarget
@onready var right_cast: RayCast2D = $rightCast
@onready var left_cast: RayCast2D = $leftCast
@onready var right_target: Node2D = $rightTarget
@onready var left_target: Node2D = $leftTarget
func _physics_process(delta: float) -> void:
@@ -16,13 +28,57 @@ func _physics_process(delta: float) -> void:
# 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("left", "right")
if Input.is_action_just_pressed("shove") && pushRightEnabled && faceLeft == false:
pushTarget.apply_central_impulse(Vector2(1,0)*PUSH_FORCE)
pushRightEnabled = false
if Input.is_action_just_pressed("shove") && pushLeftEnabled && faceLeft == true:
print("can actually push")
pushTarget.apply_central_impulse(Vector2(-1,0)*PUSH_FORCE)
pushLeftEnabled = false
# Shoot Attach
if Input.is_action_just_pressed("shoot"):
if faceLeft == false:
%SceneManager.makeBullet(right_target.global_transform, 700)
if faceLeft == true:
%SceneManager.makeBullet(left_target.global_transform, -700)
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if direction <0:
faceLeft = true
if direction >0:
faceLeft = false
move_and_slide()
if right_cast.is_colliding():
print("something on my right")
var collider = right_cast.get_collider()
if collider is Node:
if collider is RigidBody2D:
print("shove this crate")
#record that we can shove right
pushRightEnabled = true
#record what object to shove
pushTarget = collider
if left_cast.is_colliding():
print("something on my left")
var collider = left_cast.get_collider()
if collider is Node:
if collider is RigidBody2D:
print("shove this crate")
#record that we can shove left
pushLeftEnabled = true
#record what object to shove
pushTarget = collider
for i in get_slide_collision_count():
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
+17
View File
@@ -0,0 +1,17 @@
class_name Bullet extends Area2D
var speed = 750
signal hit(bullet, body)
func setSpeed(value):
speed = value
#animation
func _physics_process(delta: float) -> void:
position += transform.x * speed * delta
func _on_body_shape_entered(body_rid: RID, body: Node2D, body_shape_index: int, local_shape_index: int) -> void:
print("bullet collision")
hit.emit(self, body)
+36
View File
@@ -0,0 +1,36 @@
extends Node
var bulletsFiredTotal = 0
var bulletsMadeTotal = 0
var bulletArray= []
var bullet = preload("res://Scenes/bullet.tscn")
# bullet factory - makes bullets
func bulletFactory():
var mybullet
if bulletArray.size() < 8:
print("new bullet at factory")
mybullet = bullet.instantiate()
mybullet.connect("hit", bulletHit)
else:
print("recycled bullet at factory")
mybullet = bulletArray.pop_back()
bulletArray.push_front(mybullet)
return mybullet
# order desk for bullets
func makeBullet(position, speed):
print("make a bullet")
var newBullet = bulletFactory()
owner.add_child(newBullet)
newBullet.setSpeed(speed)
newBullet.transform = position
func bulletHit(bullet, body):
print("tell the game controller a bullet hit smthn")