week2 prep, simple bullet creation
This commit is contained in:
1 parent
d62e6ac024
commit
fcb6b1c889
8 files changed
+166
-20
No files matched your search
@@ -0,0 +1,14 @@
|
||||
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 _physics_process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
|
||||
func setSpeed(val):
|
||||
speed = val
|
||||
@@ -3,27 +3,94 @@ extends CharacterBody2D
|
||||
|
||||
const SPEED = 500.0
|
||||
const JUMP_VELOCITY = -600.0
|
||||
@export var PUSH_FORCE = 90
|
||||
@export var BUMP_FORCE = 90
|
||||
|
||||
var faceLeft:bool = false
|
||||
#Force push variables
|
||||
var pushTarget
|
||||
@export var PUSH_FORCE = 750
|
||||
var pushLeftEnabled:= false
|
||||
var pushRightEnabled:= false
|
||||
@onready var right_ray: RayCast2D = $RightRay
|
||||
@onready var left_ray: RayCast2D = $LeftRay
|
||||
|
||||
#Bullet variables
|
||||
@onready var right_target: Node2D = $RightTarget
|
||||
@onready var left_target: Node2D = $LeftTarget
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle force push
|
||||
if Input.is_action_just_pressed("push"):
|
||||
#is there an object nearby?
|
||||
if pushRightEnabled && faceLeft ==false:
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2 )
|
||||
pushRightEnabled = false
|
||||
if pushLeftEnabled && faceLeft:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2 )
|
||||
pushLeftEnabled = false
|
||||
# Handle Shoot
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("shooting")
|
||||
var newBullet = bullet.instantiate()
|
||||
owner.add_child(newBullet)
|
||||
newBullet.transform = right_target.global_transform
|
||||
if faceLeft:
|
||||
newBullet.setSpeed(-750)
|
||||
newBullet.transform = left_target.global_transform
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
if Input.is_action_just_pressed("jump") 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 direction:
|
||||
velocity.x = direction * SPEED
|
||||
#track if the player is facing right or left
|
||||
if direction > 0:
|
||||
faceLeft = false
|
||||
if direction < 0:
|
||||
faceLeft = true
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
#scan objects near player
|
||||
if right_ray.is_colliding():
|
||||
#we only care if the player is facing right
|
||||
if not faceLeft:
|
||||
var collider = right_ray.get_collider()
|
||||
#we only care if the collider is a node
|
||||
if collider is RigidBody2D:
|
||||
print("This is a rigidbody")
|
||||
#we only care if it is pushable
|
||||
if collider.is_in_group("pushables"):
|
||||
#mark the target for possible pushing
|
||||
pushRightEnabled = true
|
||||
pushTarget = collider
|
||||
if collider is Area2D:
|
||||
print("This is an area 2d")
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
|
||||
if left_ray.is_colliding():
|
||||
if faceLeft:
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
if collider.is_in_group("pushables"):
|
||||
pushLeftEnabled = true
|
||||
pushTarget = collider
|
||||
else:
|
||||
pushLeftEnabled = false
|
||||
|
||||
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() * PUSH_FORCE)
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * BUMP_FORCE)
|
||||
@@ -10,10 +10,9 @@ func _ready() -> void:
|
||||
timer.wait_time = 1
|
||||
timer.one_shot = false
|
||||
timer.connect("timeout", secondCounter)
|
||||
timer.start()
|
||||
# timer.start()
|
||||
|
||||
func secondCounter():
|
||||
print("one second")
|
||||
countdown -=1
|
||||
if countdown <=0:
|
||||
print("YOU LOSE")
|
||||
|
||||
Reference in new issue
Block a user