working force push working bullets
This commit is contained in:
1 parent
112f7ec7ee
commit
c1ff1f0a01
8 files changed
+188
-17
No files matched your search
@@ -0,0 +1,27 @@
|
||||
class_name Bullet extends Area2D
|
||||
|
||||
var speed = 750
|
||||
# did I hit anyone?
|
||||
signal hit(bullet, body)
|
||||
|
||||
func setSpeed(speedValue):
|
||||
speed = speedValue
|
||||
func _physics_process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("Bullet hit something")
|
||||
if not body.is_in_group("player"):
|
||||
hit.emit(self, body)
|
||||
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
#func _ready() -> void:
|
||||
# pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
#func _process(delta: float) -> void:
|
||||
# pass
|
||||
@@ -4,6 +4,18 @@ extends CharacterBody2D
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
const BUMP_FORCE = 50
|
||||
const PUSH_FORCE = 750
|
||||
@onready var right_ray: RayCast2D = $RightRay
|
||||
@onready var left_ray: RayCast2D = $LeftRay
|
||||
|
||||
var faceLeft = false
|
||||
|
||||
var pushTarget
|
||||
var pushLeftEnabled = false
|
||||
var pushRightEnabled = false
|
||||
@onready var right_target: Node2D = $RightTarget
|
||||
@onready var left_target: Node2D = $LeftTarget
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
@@ -17,13 +29,59 @@ 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")
|
||||
if Input.is_action_just_pressed("shove") && pushRightEnabled && faceLeft == false:
|
||||
print("shove a box")
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
|
||||
pushRightEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shove") && faceLeft == true && pushLeftEnabled == true:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
|
||||
pushLeftEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("shoot a bullet")
|
||||
var mybullet = %SceneManager.bulletFactory()
|
||||
if not faceLeft:
|
||||
mybullet.transform = right_target.global_transform
|
||||
mybullet.setSpeed(750)
|
||||
if faceLeft:
|
||||
mybullet.transform = left_target.global_transform
|
||||
mybullet.setSpeed(-750)
|
||||
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
if direction > 0:
|
||||
faceLeft = false
|
||||
if direction < 0:
|
||||
faceLeft = true
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
if right_ray.is_colliding():
|
||||
print("Something is on my right")
|
||||
if not faceLeft:
|
||||
var collider = right_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("box"):
|
||||
print("This is a pushable box")
|
||||
pushTarget = collider
|
||||
pushRightEnabled = true
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
|
||||
if left_ray.is_colliding():
|
||||
print("Something is on my left")
|
||||
if faceLeft:
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("box"):
|
||||
print("This is a pushable box")
|
||||
pushTarget = collider
|
||||
pushLeftEnabled = true
|
||||
else:
|
||||
pushLeftEnabled = false
|
||||
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
extends Node
|
||||
|
||||
var bulletsFiredTotal := 0
|
||||
var bulletsMadeTotal := 0
|
||||
var bulletArray:Array = []
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
|
||||
func bulletFactory():
|
||||
print("Make a bullet at the factory")
|
||||
var mybullet
|
||||
|
||||
# How many bullet have been made
|
||||
if bulletArray.size() < 4:
|
||||
# make a new bullet
|
||||
mybullet = bullet.instantiate()
|
||||
mybullet.connect("hit", onBulletHit)
|
||||
owner.add_child(mybullet)
|
||||
else:
|
||||
mybullet = bulletArray.pop_back()
|
||||
bulletArray.push_front(mybullet)
|
||||
bulletsMadeTotal += 1
|
||||
return mybullet
|
||||
|
||||
func onBulletHit(bullet, body):
|
||||
print("Scene Manager knows a bullet hit something")
|
||||
bullet.setSpeed(0)
|
||||
|
||||
if body.is_in_group("shootable"):
|
||||
print("explode this thing")
|
||||
body.queue_free()
|
||||
Reference in new issue
Block a user