bullets, bullet factory, shove attack, scenemanager
This commit is contained in:
1 parent
a98540de07
commit
8a78da855c
9 files changed
+166
-16
No files matched your search
@@ -4,6 +4,17 @@ extends CharacterBody2D
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
|
||||
@onready var right_cast: RayCast2D = $RightCast
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Node2D = $RightSpawn
|
||||
@onready var left_spawn: Node2D = $LeftSpawn
|
||||
|
||||
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
|
||||
var pushTarget
|
||||
var pushEnabled = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
@@ -13,12 +24,32 @@ func _physics_process(delta: float) -> void:
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
if Input.is_action_just_pressed("shove") && pushEnabled:
|
||||
print("shove pressed")
|
||||
if facing == FaceDirection.RIGHT:
|
||||
pushTarget.apply_central_impulse(Vector2(1,0)*700)
|
||||
pushEnabled = false
|
||||
if facing == FaceDirection.LEFT:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0)*700)
|
||||
pushEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("shoot a bullet")
|
||||
if facing == FaceDirection.RIGHT:
|
||||
%SceneManager.makeBullet(right_spawn.global_transform, 700)
|
||||
if facing == FaceDirection.LEFT:
|
||||
%SceneManager.makeBullet(left_spawn.global_transform, -700)
|
||||
|
||||
# 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")
|
||||
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)
|
||||
|
||||
@@ -27,3 +58,23 @@ func _physics_process(delta: float) -> void:
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
||||
|
||||
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
|
||||
#get the thing I am colliding with
|
||||
var collider = right_cast.get_collider()
|
||||
if collider is Node && collider is RigidBody2D:
|
||||
print("I can shove this to the right")
|
||||
pushTarget = collider
|
||||
pushEnabled = true
|
||||
if not right_cast.is_colliding() && not left_cast.is_colliding():
|
||||
pushEnabled = false
|
||||
if left_cast.is_colliding() && facing==FaceDirection.LEFT:
|
||||
var collider = left_cast.get_collider()
|
||||
if collider is Node && collider is RigidBody2D:
|
||||
print("I can shove this to the left")
|
||||
pushTarget = collider
|
||||
pushEnabled = true
|
||||
if not left_cast.is_colliding() && not right_cast.is_colliding():
|
||||
pushEnabled = false
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
extends Node2D
|
||||
|
||||
var bullet = preload("res://Scenes/bullet.tscn")
|
||||
var bulletArray =[]
|
||||
var totalAllowedBullets = 7
|
||||
|
||||
#makes bullets
|
||||
func bulletFactory():
|
||||
var mybullet
|
||||
if bulletArray.size() < totalAllowedBullets:
|
||||
#make a new bullet
|
||||
mybullet = bullet.instantiate()
|
||||
owner.add_child(mybullet)
|
||||
else:
|
||||
#recycle bullet
|
||||
mybullet = bulletArray.pop_back()
|
||||
|
||||
bulletArray.push_front(mybullet)
|
||||
return mybullet
|
||||
|
||||
#order desk
|
||||
func makeBullet(position, speed):
|
||||
print("Scenemanger orders a bullet")
|
||||
#ask the factory for a bullet
|
||||
var mybullet = bulletFactory()
|
||||
#set the speed for the bullet
|
||||
mybullet.setSpeed(speed)
|
||||
#position the bullet
|
||||
mybullet.transform = position
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://bs4frobn6kxne
|
||||
Reference in new issue
Block a user