aug 11
This commit is contained in:
14 files changed
+213
-54
No files matched your search
@@ -0,0 +1,16 @@
|
||||
class_name Bullet extends Area2D
|
||||
|
||||
var speed: float = 700
|
||||
signal bulletDamageSignal(target: Node2D, bullet: Node2D)
|
||||
|
||||
func setSpeed(val: float):
|
||||
speed = val
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if body is Player:
|
||||
return
|
||||
if body is Crate && visible:
|
||||
bulletDamageSignal.emit(body, self)
|
||||
@@ -0,0 +1 @@
|
||||
uid://bemhplfq3bapd
|
||||
@@ -0,0 +1 @@
|
||||
class_name Crate extends RigidBody2D
|
||||
@@ -0,0 +1 @@
|
||||
uid://caikxm70kr5wu
|
||||
@@ -11,7 +11,15 @@ func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_area_2d_area_trigger_signal(effect: Variant, body: Variant) -> void:
|
||||
print("GC sees trigger " + effect)
|
||||
if not body is Player:
|
||||
body.queue_free()
|
||||
func _on_area_2d_area_trigger_signal(effect: String, body: Node2D) -> void:
|
||||
if body.name == "world-boundary":
|
||||
return
|
||||
if body is Player:
|
||||
return
|
||||
print("GC sees trigger " + effect + " on " + body.name)
|
||||
body.queue_free()
|
||||
|
||||
func bulletDamage(target: Node2D, bullet: Node2D):
|
||||
print("game controller sees hit")
|
||||
target.queue_free()
|
||||
bullet.visible = false
|
||||
+66
-5
@@ -1,30 +1,91 @@
|
||||
class name Player extends CharacterBody2D
|
||||
class_name Player extends CharacterBody2D
|
||||
|
||||
const bullet_scene = preload("res://scenes/bullet.tscn")
|
||||
|
||||
@onready var right_cast: RayCast2D = $RightCast
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var bullet_spawn_point_right: Node2D = $BulletSpawnPointRight
|
||||
@onready var bullet_spawn_point_left: Node2D = $BulletSpawnPointLeft
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
const PUSH_POWER= 2000
|
||||
|
||||
var direction: float
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing: FaceDirection = FaceDirection.RIGHT
|
||||
var pushTarget: Object
|
||||
var pushEnabled: bool = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
handle_input(delta)
|
||||
handle_movement(delta)
|
||||
handle_collisions(delta)
|
||||
|
||||
func handle_gravity(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
|
||||
func handle_input(delta: float) -> void:
|
||||
var dir: int = 0
|
||||
match facing:
|
||||
FaceDirection.LEFT:
|
||||
dir = -1
|
||||
FaceDirection.RIGHT:
|
||||
dir = 1
|
||||
|
||||
# Handle jump.
|
||||
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
|
||||
elif direction > 0:
|
||||
facing = FaceDirection.RIGHT
|
||||
|
||||
if Input.is_action_just_pressed("ForcePush") && pushEnabled:
|
||||
(pushTarget as RigidBody2D).apply_central_impulse(Vector2(dir, 0) * PUSH_POWER)
|
||||
|
||||
if Input.is_action_just_pressed("Shoot"):
|
||||
var spawnAt: Node2D = bullet_spawn_point_left
|
||||
if dir == 1:
|
||||
spawnAt = bullet_spawn_point_right
|
||||
|
||||
#var b = bullet_scene.instantiate()
|
||||
#b.position = spawnAt.position
|
||||
print("make a bullet")
|
||||
var b = %SceneManager.makeBullet(spawnAt.global_transform, dir)
|
||||
#b.position = Player.
|
||||
#b.Position = Player.Position
|
||||
|
||||
|
||||
func handle_movement(delta: float) -> void:
|
||||
handle_gravity(delta)
|
||||
# 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:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
|
||||
func handle_collisions(delta: float) -> void:
|
||||
for i in get_slide_collision_count():
|
||||
var coll = get_slide_collision(i)
|
||||
if coll.get_collider() is RigidBody2D:
|
||||
coll.get_collider().apply_central_impulse(-coll.get_normal() * 100)
|
||||
|
||||
if right_cast.is_colliding() && facing == FaceDirection.RIGHT:
|
||||
var col = right_cast.get_collider()
|
||||
if col != null && col.is_in_group("pushable"):
|
||||
pushTarget = col
|
||||
pushEnabled = true
|
||||
elif left_cast.is_colliding() && facing == FaceDirection.LEFT:
|
||||
var col = left_cast.get_collider()
|
||||
if col != null && col.is_in_group("pushable"):
|
||||
pushTarget = col
|
||||
pushEnabled = true
|
||||
else:
|
||||
pushEnabled = false
|
||||
pushTarget = null
|
||||
@@ -0,0 +1,32 @@
|
||||
class_name SceneManager extends Node2D
|
||||
|
||||
const BulletScn = preload("res://scenes/bullet.tscn")
|
||||
@onready var game: Node2D = $".."
|
||||
|
||||
var bullets: Array[Bullet] = []
|
||||
const MAX_BULLETS = 10
|
||||
var current_bullet = 0
|
||||
|
||||
# makes and recycles bullets
|
||||
func bulletFactory() -> Bullet:
|
||||
if len(bullets) < MAX_BULLETS:
|
||||
# create new
|
||||
var b = BulletScn.instantiate()
|
||||
b.bulletDamageSignal.connect(game.bulletDamage)
|
||||
add_child(b)
|
||||
bullets.append(b)
|
||||
return b
|
||||
else:
|
||||
# recycle
|
||||
var i = current_bullet
|
||||
current_bullet = (current_bullet + 1) % MAX_BULLETS
|
||||
bullets[i].visible = true
|
||||
return bullets[i]
|
||||
|
||||
|
||||
func makeBullet(pos: Transform2D, dir: int) -> Node2D:
|
||||
var b = bulletFactory()
|
||||
#b.position = pos.get_origin()
|
||||
b.transform = pos
|
||||
b.setSpeed(dir * 700)
|
||||
return b
|
||||
@@ -0,0 +1 @@
|
||||
uid://slrv7nc7hjkl
|
||||
@@ -6,5 +6,4 @@ extends Area2D
|
||||
signal areaTriggerSignal(effect, Object)
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("trigger sees a body")
|
||||
areaTriggerSignal.emit(effect, body)
|
||||
Reference in new issue
Block a user