added bullet and bullet scripts
This commit is contained in:
1 parent
ac8b1c1008
commit
f78851ae8c
113 files changed
+1286
-116
No files matched your search
@@ -0,0 +1,16 @@
|
||||
class_name Bullet extends Area2D
|
||||
var speed = 700
|
||||
|
||||
signal bulletHit(body, bullet)
|
||||
|
||||
func setSpeed(value):
|
||||
speed = value
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if not body is Player:
|
||||
print ("bullet hit! ow!" )
|
||||
bulletHit.emit(body, self)
|
||||
@@ -0,0 +1 @@
|
||||
uid://bs8ylc2565i2g
|
||||
@@ -0,0 +1 @@
|
||||
class_name Crate extends RigidBody2D
|
||||
@@ -0,0 +1 @@
|
||||
uid://bvmsaiok85e3q
|
||||
@@ -29,3 +29,10 @@ func _on_trigger_fired(effect: Variant, body: Variant) -> void:
|
||||
if totalCrates==0:
|
||||
print("YOU WIN")
|
||||
get_tree().reload_current_scene()
|
||||
|
||||
func numberOfCrates(value):
|
||||
totalCrates = value
|
||||
print ("Total crates at GC: "+str(totalCrates))
|
||||
|
||||
func bulletDamage(body, bullet):
|
||||
print ("game controller knows about bullet hit")
|
||||
+67
-6
@@ -1,17 +1,52 @@
|
||||
extends CharacterBody2D
|
||||
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
|
||||
|
||||
@export var pushpower = 80
|
||||
@export var bump_power = 80
|
||||
@export var shove_power = 500
|
||||
|
||||
enum FaceDirection {LEFT, RIGHT}
|
||||
var facing: FaceDirection = FaceDirection.RIGHT
|
||||
|
||||
var pushTarget:RigidBody2D
|
||||
var pushEnabled:bool = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
|
||||
handle_input()
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_input() -> void:
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print ("shoot a bullet")
|
||||
if facing == FaceDirection.LEFT:
|
||||
%SceneManager.makeBullet(left_spawn.global_transform, -700)
|
||||
if facing == FaceDirection.RIGHT:
|
||||
%SceneManager.makeBullet(right_spawn.global_transform,700)
|
||||
if Input.is_action_just_pressed("shove") && pushEnabled:
|
||||
print ("shove attack!")
|
||||
if facing == FaceDirection.LEFT:
|
||||
print ("facing left")
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * shove_power)
|
||||
pushEnabled = false
|
||||
if facing == FaceDirection.RIGHT:
|
||||
print ("facing right")
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * shove_power)
|
||||
pushEnabled = false
|
||||
if not right_cast.is_colliding() and not left_cast.is_colliding():
|
||||
pushEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
@@ -20,11 +55,37 @@ func _physics_process(delta: float) -> void:
|
||||
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()
|
||||
|
||||
func update_movement() -> void:
|
||||
pass
|
||||
|
||||
func update_states() -> void:
|
||||
pass
|
||||
|
||||
func handle_collisions() -> 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() * pushpower)
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * bump_power)
|
||||
if right_cast.is_colliding() && facing ==FaceDirection.RIGHT:
|
||||
print ("right raycast is hitting something")
|
||||
var collider = right_cast.get_collider()
|
||||
if collider is Node && collider is RigidBody2D:
|
||||
print ("I think it is a crate on my right")
|
||||
pushTarget = collider
|
||||
pushEnabled = true
|
||||
if left_cast.is_colliding() && facing ==FaceDirection.LEFT:
|
||||
print ("left raycast is hitting something")
|
||||
var collider = left_cast.get_collider()
|
||||
if collider is Node && collider is RigidBody2D:
|
||||
print ("I think it is a crate on my left")
|
||||
pushTarget = collider
|
||||
pushEnabled = true
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
extends Node
|
||||
@onready var game: GameController = $".."
|
||||
@onready var crates: Node2D = $"../Crates"
|
||||
|
||||
var bullet = preload("res://Scenes/Bullet.tscn")
|
||||
var bulletArray=[]
|
||||
var totalAllowedBullets = 7
|
||||
|
||||
func buildLevel () -> void:
|
||||
#count crates
|
||||
var totalCrates = 0
|
||||
for obj in crates.get_children():
|
||||
if obj is Crate:
|
||||
totalCrates +=1
|
||||
#tell the game controller
|
||||
game.numberOfCrates(totalCrates)
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
buildLevel()
|
||||
|
||||
func bulletFarm():
|
||||
var mybullet
|
||||
if bulletArray.size() < totalAllowedBullets:
|
||||
mybullet = bullet.instantiate()
|
||||
mybullet.connect("bulletHit", game.bulletDamage)
|
||||
owner.add_child(mybullet)
|
||||
else:
|
||||
#recyclebullet\
|
||||
mybullet = bulletArray.pop_back()
|
||||
|
||||
bulletArray.push_front(mybullet)
|
||||
return mybullet
|
||||
|
||||
func makeBullet(position, speed):
|
||||
print ("scene manager makes a bullet")
|
||||
var mybullet = bulletFarm()
|
||||
mybullet.setSpeed(speed)
|
||||
mybullet.transform = position
|
||||
@@ -0,0 +1 @@
|
||||
uid://c838c4s60bkfg
|
||||
Reference in new issue
Block a user