raycasts, melee attack, factory for bullets, spawn points, dynamic instantiation
This commit is contained in:
1 parent
2b2d2f08e5
commit
b6d588fcfe
13 files changed
+202
-42
No files matched your search
@@ -0,0 +1,18 @@
|
||||
class_name Bullet extends Area2D
|
||||
|
||||
|
||||
var speed:float = 700
|
||||
signal bulletDamageSignal(body, bullet)
|
||||
|
||||
func setSpeed(value:float):
|
||||
speed = value
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("Bullet hitting")
|
||||
if body.is_in_group("shootables"):
|
||||
print("This is shootable")
|
||||
bulletDamageSignal.emit(body, self)
|
||||
@@ -0,0 +1 @@
|
||||
uid://bhh178vuo5bgo
|
||||
@@ -0,0 +1 @@
|
||||
class_name Crate extends RigidBody2D
|
||||
@@ -0,0 +1 @@
|
||||
uid://c5gjsxgxmjfki
|
||||
@@ -16,3 +16,7 @@ func _on_trigger(effect: Variant, body: Variant) -> void:
|
||||
if not body is Player:
|
||||
body.queue_free()
|
||||
|
||||
func bulletDamage(body, bullet):
|
||||
print("GC know about bullet hit")
|
||||
body.queue_free()
|
||||
|
||||
+60
-11
@@ -1,29 +1,78 @@
|
||||
class_name Player extends CharacterBody2D
|
||||
|
||||
@onready var right_cast: RayCast2D = $RightCast
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Node2D = $RightSpawn
|
||||
@onready var left_spawn: Node2D = $LeftSpawn
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
|
||||
var direction
|
||||
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
|
||||
|
||||
# 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")
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_movement(delta:float) -> void:
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
func handle_input():
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
direction = Input.get_axis("ui_left", "ui_right")
|
||||
if direction < 0:
|
||||
facing = FaceDirection.LEFT
|
||||
if direction > 0:
|
||||
facing = FaceDirection.RIGHT
|
||||
if Input.is_action_just_pressed("shove") && pushEnabled:
|
||||
var shoveDirection:int
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
shoveDirection = 1
|
||||
FaceDirection.LEFT:
|
||||
shoveDirection = -1
|
||||
pushTarget.apply_central_impulse(Vector2(shoveDirection,0)*700)
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("Ima shoot")
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
print("shoot to right")
|
||||
%SceneManager.makeBullet(right_spawn.global_transform, 700)
|
||||
FaceDirection.LEFT:
|
||||
%SceneManager.makeBullet(left_spawn.global_transform, -700)
|
||||
|
||||
move_and_slide()
|
||||
func handle_collisions():
|
||||
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() * 100)
|
||||
|
||||
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
|
||||
print("RC is colliding")
|
||||
var collider = right_cast.get_collider()
|
||||
if collider is Node && collider is RigidBody2D && collider.is_in_group("pushables"):
|
||||
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 && collider.is_in_group("pushables"):
|
||||
pushTarget = collider
|
||||
pushEnabled = true
|
||||
|
||||
if not right_cast.is_colliding() && not left_cast.is_colliding():
|
||||
pushEnabled = false
|
||||
@@ -0,0 +1,34 @@
|
||||
class_name SceneManager extends Node2D
|
||||
var bulletArray =[]
|
||||
var totalAllowedBullets = 7
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
@onready var game: Node2D = $".."
|
||||
|
||||
# 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
|
||||
|
||||
# makes and recycles bullets
|
||||
func bulletFactory():
|
||||
var myBullet:Bullet
|
||||
if bulletArray.size() < totalAllowedBullets:
|
||||
#make a new bullet
|
||||
myBullet = bullet.instantiate()
|
||||
myBullet.bulletDamageSignal.connect(game.bulletDamage)
|
||||
owner.add_child(myBullet)
|
||||
else:
|
||||
myBullet = bulletArray.pop_back()
|
||||
|
||||
bulletArray.push_front(myBullet)
|
||||
return myBullet
|
||||
|
||||
func makeBullet(position, speed):
|
||||
var someBullet = bulletFactory()
|
||||
someBullet.setSpeed(speed)
|
||||
# postition the bullet
|
||||
someBullet.transform = position
|
||||
@@ -0,0 +1 @@
|
||||
uid://d1daji4xwuj03
|
||||
Reference in new issue
Block a user