bullet factory, push attack
This commit is contained in:
1 parent
b5e5db7ef1
commit
de7312cf8e
7 files changed
+272
-63
No files matched your search
@@ -3,6 +3,19 @@ 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_target: Node2D = $rightTarget_Node2D
|
||||
@onready var left_target: Node2D = $leftTarget_Node2D
|
||||
|
||||
@onready var right_cast: RayCast2D = $right_RayCast2D
|
||||
@onready var left_cast: RayCast2D = $left_RayCast2D
|
||||
|
||||
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
@@ -10,20 +23,60 @@ func _physics_process(delta: float) -> void:
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
# Handle jump.right_cast
|
||||
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("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 we 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:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * 60)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
extends Node
|
||||
|
||||
var bulletsFiredTotal = 0
|
||||
var bulletsMadeTotal = 0
|
||||
var bulletArray = []
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
|
||||
# Bullet factor - makes bullets
|
||||
func bulletFactory():
|
||||
var mybullet
|
||||
if bulletArray.size()<4:
|
||||
print("new bullet at factory")
|
||||
mybullet = bullet.instantiate()
|
||||
mybullet.connect("hit", bulletHit)
|
||||
else:
|
||||
mybullet = bulletArray.pop_back()
|
||||
print("recycled bullet")
|
||||
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 game controller we hit something")
|
||||
Reference in new issue
Block a user