bullets and force push working

This commit is contained in:
winniewk committed 2024-09-09 21:02:15 -04:00
1 parent 8428eebd76
commit 0c164bff8a
7 files changed
+148 -25

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
extends Area2D
var speed := 700
signal hit(bullet, body)
func setSpeed(speedVal):
speed = speedVal
func _physics_process(delta: float) -> void:
position += transform.x * speed * delta
func _on_body_entered(body: Node2D) -> void:
print("Hit")
+71 -2
View File
@@ -3,6 +3,20 @@ extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
const PUSH_FORCE = 700
var faceLeft = false
# can I push right or left
var pushLeftEnabled = false
var pushRightEnabled = false
@onready var right_ray: RayCast2D = $RightRay
@onready var left_ray: RayCast2D = $LeftRay
@onready var marker_right: Node2D = $MarkerRight
@onready var marker_left: Node2D = $MarkerLeft
var pushTarget
var bullet = preload("res://Scenes/Bullet.tscn")
func _physics_process(delta: float) -> void:
@@ -13,10 +27,43 @@ 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") && pushLeftEnabled && faceLeft:
print("Shove left")
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 1.5)
pushLeftEnabled = false
if Input.is_action_just_pressed("Shove") && pushRightEnabled && not faceLeft:
print("Shove right")
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 1.5)
pushRightEnabled = false
if Input.is_action_just_pressed("Shoot"):
print ("Shooting")
var mybullet = bullet.instantiate()
if faceLeft:
print("shoot left")
mybullet.setSpeed(-700)
#position bullet
mybullet.transform = marker_left.global_transform
else:
print("shoot right")
#position bullet
mybullet.transform = marker_right.global_transform
#add bullet
owner.add_child(mybullet)
# 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 <0:
faceLeft = true
if direction >0:
faceLeft = false
if direction:
velocity.x = direction * SPEED
else:
@@ -26,5 +73,27 @@ func _physics_process(delta: float) -> void:
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()* 200)
c.get_collider().apply_central_impulse(-c.get_normal()* 50)
if left_ray.is_colliding():
print("left ray collistion")
var collider = left_ray.get_collider()
if collider is Node:
if collider.is_in_group("Pushable"):
pushLeftEnabled = true
pushTarget = collider
else:
# do somthing else
pushLeftEnabled = false
if right_ray.is_colliding():
print("right ray collistion")
var collider = right_ray.get_collider()
if collider is Node:
if collider.is_in_group("Pushable"):
pushRightEnabled = true
pushTarget = collider
else:
pushRightEnabled = false