bullet factory, push attack
This commit is contained in:
1 parent
7eca676464
commit
1ed49408f6
7 files changed
+249
-81
No files matched your search
@@ -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_entered(body: Node2D) -> void:
|
||||
print("bullet collision")
|
||||
hit.emit(self, body)
|
||||
@@ -3,6 +3,18 @@ extends CharacterBody2D
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.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,50 @@ 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:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE)
|
||||
pushLeftEnabled = false
|
||||
# shoot attack
|
||||
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():
|
||||
var collider = left_cast.get_collider()
|
||||
if collider is Node:
|
||||
if collider is RigidBody2D:
|
||||
pushLeftEnabled = true
|
||||
pushTarget = collider
|
||||
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
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() < 7:
|
||||
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 something")
|
||||
Reference in new issue
Block a user