bullets, bulletfactory, shove attack, scenemanager
This commit is contained in:
1 parent
e771dd5a61
commit
0afb528430
11 files changed
+188
-44
No files matched your search
@@ -0,0 +1,13 @@
|
||||
class_name Bullet extends Area2D
|
||||
var speed = -700
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
func setSpeed(value):
|
||||
speed = value
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _physics_process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
@@ -0,0 +1 @@
|
||||
uid://b8fowda770nwo
|
||||
@@ -0,0 +1,75 @@
|
||||
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.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
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)
|
||||
|
||||
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
|
||||
# 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")
|
||||
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)
|
||||
|
||||
move_and_slide()
|
||||
for inc in get_slide_collision_count():
|
||||
var col = get_slide_collision(inc)
|
||||
if col.get_collider() is RigidBody2D:
|
||||
col.get_collider().apply_central_impulse(-col.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 (crate)")
|
||||
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:
|
||||
print("I can shove this to the left")
|
||||
pushTarget = collider
|
||||
pushEnabled = true
|
||||
if not right_cast.is_colliding() && not left_cast.is_colliding():
|
||||
pushEnabled = false
|
||||
@@ -0,0 +1 @@
|
||||
uid://dmyw2bn6gnbbv
|
||||
@@ -0,0 +1,29 @@
|
||||
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("Scenemanager 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://cf8f01b63m2hq
|
||||
Reference in new issue
Block a user