Week 2 commits: force push, multiple triggers, force push, teleporting crates.
This commit is contained in:
1 parent
8b43b3c4f9
commit
458b3e25fc
13 files changed
+277
-50
No files matched your search
@@ -0,0 +1,15 @@
|
||||
class_name Bullet extends Area2D
|
||||
|
||||
var speed:float = 700
|
||||
signal bulletHitSignal(body, bullet)
|
||||
|
||||
func setSpeed(value) -> void:
|
||||
speed = value
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
position += transform.x * speed * delta
|
||||
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if body is Crate:
|
||||
bulletHitSignal.emit(body, self)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dcqok1j5pliwd
|
||||
@@ -1 +1,12 @@
|
||||
class_name Crate extends RigidBody2D
|
||||
var teleport_target:Vector2 = Vector2.ZERO
|
||||
var should_teleport:bool = false
|
||||
|
||||
func teleport_to(position:Vector2):
|
||||
teleport_target = position
|
||||
should_teleport = true
|
||||
|
||||
func _integrate_forces(state: PhysicsDirectBodyState2D) -> void:
|
||||
if should_teleport:
|
||||
state.transform = Transform2D.IDENTITY.translated(teleport_target)
|
||||
should_teleport = false
|
||||
+23
-8
@@ -1,11 +1,15 @@
|
||||
extends Node2D
|
||||
class_name GameController extends Node2D
|
||||
signal destroySignal(body)
|
||||
signal fanTriggerSignal(body, fanDirection)
|
||||
signal teleportTriggerSignal(body)
|
||||
|
||||
var numberOfCrates = 2
|
||||
var timeAvailable = 5
|
||||
var timeAvailable = 50
|
||||
var timer = Timer.new()
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
get_window().grab_focus()
|
||||
add_child(timer)
|
||||
timer.wait_time = 1
|
||||
timer.one_shot = false
|
||||
@@ -24,14 +28,25 @@ func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_trigger(body: Variant, trigger: Variant) -> void:
|
||||
func _on_trigger(body: Variant, trigger: Variant, intent: String) -> void:
|
||||
print("Game controller knows trigger fired")
|
||||
body.queue_free()
|
||||
numberOfCrates -=1
|
||||
if numberOfCrates == 0:
|
||||
print("WINNER!!!")
|
||||
get_tree().reload_current_scene()
|
||||
match intent:
|
||||
"destroy":
|
||||
destroySignal.emit(body)
|
||||
"fan":
|
||||
fanTriggerSignal.emit(body,Vector2(0,-1)*100*9)
|
||||
print("fan effect")
|
||||
"teleport":
|
||||
print("teleport effect")
|
||||
teleportTriggerSignal.emit(body)
|
||||
|
||||
func crateTotal(numOfCrates):
|
||||
numberOfCrates = numOfCrates
|
||||
print("Game controller knows about crates - there are: " + str(numOfCrates))
|
||||
if numberOfCrates == 0:
|
||||
print("WINNER!!!")
|
||||
get_tree().reload_current_scene()
|
||||
|
||||
func bulletDamage(body, _bullet) -> void:
|
||||
if body.is_in_group("shootable"):
|
||||
destroySignal.emit(body)
|
||||
+61
-2
@@ -1,9 +1,22 @@
|
||||
extends CharacterBody2D
|
||||
class_name Player extends CharacterBody2D
|
||||
signal pushSignal(body,direction)
|
||||
signal shootSignal(markerPosition, speed)
|
||||
|
||||
@onready var right_cast: RayCast2D = $RightCast
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_bullet_spawn: Marker2D = $RightBulletSpawn
|
||||
@onready var left_bullet_spawn: Marker2D = $LeftBulletSpawn
|
||||
|
||||
var pushRightEnabled = false
|
||||
var pushLeftEnabled = false
|
||||
var pushTarget
|
||||
var pushTimer = 0
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
|
||||
enum FaceDirection {RIGHT,LEFT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
@@ -13,17 +26,63 @@ func _physics_process(delta: float) -> void:
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_up") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# Handle shove
|
||||
if Input.is_action_just_pressed("shove"):
|
||||
print("Shove pressed")
|
||||
pushTimer = Time.get_ticks_msec()
|
||||
if Input.is_action_just_released("shove"):
|
||||
var pushForce = Time.get_ticks_msec() - pushTimer
|
||||
if pushForce > 4000:
|
||||
pushForce = 4000
|
||||
if pushRightEnabled and facing == FaceDirection.RIGHT:
|
||||
pushSignal.emit(pushTarget, Vector2(1,0) * 100 * (pushForce/100))
|
||||
if pushLeftEnabled and facing == FaceDirection.LEFT:
|
||||
pushSignal.emit(pushTarget, Vector2(-1,0) * 100 * (pushForce/100))
|
||||
|
||||
# Handle shoot
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("Shoot pressed")
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
shootSignal.emit(right_bullet_spawn.global_transform, 800)
|
||||
FaceDirection.LEFT:
|
||||
shootSignal.emit(left_bullet_spawn.global_transform, 800)
|
||||
|
||||
# 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
|
||||
if direction > 0:
|
||||
facing = FaceDirection.RIGHT
|
||||
else:
|
||||
facing = FaceDirection.LEFT
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
# Collision detection needs to happen AFTER move_and_slide calculation
|
||||
if right_cast.is_colliding():
|
||||
print("Right raycast collision")
|
||||
var collider = right_cast.get_collider()
|
||||
if collider is Node:
|
||||
pushRightEnabled = true
|
||||
pushTarget = collider
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
|
||||
if left_cast.is_colliding():
|
||||
print("Left raycast collision")
|
||||
var collider = left_cast.get_collider()
|
||||
if collider is Node:
|
||||
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() * 100)
|
||||
#c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
||||
pushSignal.emit(c.get_collider(),-c.get_normal() * 100)
|
||||
@@ -1,9 +1,21 @@
|
||||
extends Node2D
|
||||
@onready var game: Node2D = $".."
|
||||
@onready var game: GameController = $".."
|
||||
@onready var crates: Node2D = $"../Crates"
|
||||
@onready var player: Player = $"../Player"
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
var bulletArray:Array[Bullet] = []
|
||||
var totalAllowedBullet:int = 7
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
# signals from the view
|
||||
player.pushSignal.connect(pushTarget)
|
||||
player.shootSignal.connect(makeBullet)
|
||||
# signals from the game controller
|
||||
game.destroySignal.connect(destroy)
|
||||
game.fanTriggerSignal.connect(pushTarget)
|
||||
game.teleportTriggerSignal.connect(teleportTarget)
|
||||
buildLevel()
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
@@ -11,10 +23,47 @@ func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func buildLevel() -> void:
|
||||
# tell game controller how many crates
|
||||
updateCrates()
|
||||
|
||||
func updateCrates() -> void:
|
||||
var totalCrates = 0
|
||||
if crates:
|
||||
for obj in crates.get_children():
|
||||
if obj is Crate:
|
||||
if not obj.tree_exited.is_connected(updateCrates):
|
||||
obj.tree_exited.connect(updateCrates)
|
||||
totalCrates += 1
|
||||
game.crateTotal(totalCrates)
|
||||
|
||||
func pushTarget(body,pushDirection) -> void:
|
||||
if body is RigidBody2D:
|
||||
body.apply_central_impulse(pushDirection)
|
||||
|
||||
func destroy(body) -> void:
|
||||
body.queue_free()
|
||||
|
||||
func teleportTarget(body) -> void:
|
||||
if body is Crate:
|
||||
var viewport_size = get_viewport().get_visible_rect().size
|
||||
var random_x = randf_range(0,viewport_size.x)
|
||||
var random_y = randf_range(0,viewport_size.y)
|
||||
var newPosition = Vector2(random_x, random_y)
|
||||
body.teleport_to(newPosition)
|
||||
|
||||
func bulletFactory() -> Bullet:
|
||||
var myBullet:Bullet
|
||||
if bulletArray.size() <= totalAllowedBullet:
|
||||
myBullet = bullet.instantiate()
|
||||
add_sibling(myBullet)
|
||||
if not myBullet.bulletHitSignal.is_connected(game.bulletDamage):
|
||||
myBullet.bulletHitSignal.connect(game.bulletDamage)
|
||||
else:
|
||||
myBullet = bulletArray.pop_back()
|
||||
bulletArray.push_front(myBullet)
|
||||
return myBullet
|
||||
|
||||
func makeBullet(spawnPosition, speed) -> void:
|
||||
print("make bullet")
|
||||
var myBullet:Bullet = bulletFactory()
|
||||
myBullet.transform = spawnPosition
|
||||
myBullet.setSpeed(speed)
|
||||
+6
-3
@@ -1,5 +1,8 @@
|
||||
extends Area2D
|
||||
signal areaTriggerSignal(body, trigger)
|
||||
class_name Trigger extends Area2D
|
||||
|
||||
@export var intent = "destroy"
|
||||
|
||||
signal areaTriggerSignal(body, trigger, intent)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
@@ -13,4 +16,4 @@ func _process(delta: float) -> void:
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("Something has entered the trigger zone")
|
||||
areaTriggerSignal.emit(body, self)
|
||||
areaTriggerSignal.emit(body, self, intent)
|
||||
Reference in new issue
Block a user