bullet, timer, scenemanager keyboard input
This commit is contained in:
1 parent
76b6721c42
commit
df3ebe62d2
12 files changed
+231
-46
No files matched your search
@@ -0,0 +1,12 @@
|
||||
class_name Bullet extends Area2D
|
||||
|
||||
var speed:float = 700
|
||||
|
||||
# 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:
|
||||
position += transform.x * speed *delta
|
||||
@@ -0,0 +1 @@
|
||||
uid://bfuhpow7x2xr1
|
||||
@@ -1 +1,13 @@
|
||||
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
|
||||
@@ -1,18 +1,34 @@
|
||||
extends Node2D
|
||||
class_name GameController extends Node2D
|
||||
|
||||
var crateTotal = 3
|
||||
var crateTotal = 0
|
||||
signal destroySignal(body)
|
||||
signal teleportSignal(body)
|
||||
signal levelChangeSignal(level)
|
||||
var currentScene:String = "res://scenes/game.tscn"
|
||||
|
||||
var timer := Timer.new()
|
||||
var timeAvailable := 10
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
add_child(timer)
|
||||
timer.wait_time = 1
|
||||
timer.one_shot= false
|
||||
timer.connect("timeout", secondCounter)
|
||||
timer.start()
|
||||
|
||||
func secondCounter()->void:
|
||||
timeAvailable -=1
|
||||
if timeAvailable <=0:
|
||||
print("YOU LOSE BABY!!")
|
||||
levelChangeSignal.emit(currentScene)
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_trigger(body: Variant, effect) -> void:
|
||||
func _on_trigger(body: Variant, effect, trigger) -> void:
|
||||
print("GC knows trigger...."+effect)
|
||||
if body is Crate:
|
||||
match effect:
|
||||
@@ -20,4 +36,19 @@ func _on_trigger(body: Variant, effect) -> void:
|
||||
crateTotal -=1
|
||||
if crateTotal <=0:
|
||||
print("You WON!!!")
|
||||
body.queue_free()
|
||||
levelChangeSignal.emit(currentScene)
|
||||
destroySignal.emit(body)
|
||||
"teleport":
|
||||
print("GC teleport an object")
|
||||
teleportSignal.emit(body)
|
||||
|
||||
if body is Player:
|
||||
match effect:
|
||||
"powerup":
|
||||
print("GC knows about powerup")
|
||||
timeAvailable +=5
|
||||
destroySignal.emit(trigger)
|
||||
|
||||
func crateUpdate(cratesAmount)->void:
|
||||
crateTotal = cratesAmount
|
||||
print("GC updated crates: "+str(crateTotal))
|
||||
+21
-3
@@ -1,16 +1,30 @@
|
||||
extends CharacterBody2D
|
||||
class_name Player extends CharacterBody2D
|
||||
|
||||
@onready var right_target: Marker2D = $RightTarget
|
||||
@onready var left_target: Marker2D = $LeftTarget
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
const BUMP_POWER = 50
|
||||
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle Shoot
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("Player wants to shoot")
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
print("Shoot to the right")
|
||||
%SceneManager.makeBullet(right_target.global_transform, 700)
|
||||
FaceDirection.LEFT:
|
||||
print("Shoot to the left")
|
||||
%SceneManager.makeBullet(left_target.global_transform, -700)
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
@@ -20,6 +34,10 @@ 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)
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
class_name SceneManager extends Node2D
|
||||
@onready var triggers: Node2D = $"../Triggers"
|
||||
@onready var crates: Node2D = $"../Crates"
|
||||
@onready var level: Node2D = $"../Level"
|
||||
@onready var game: GameController = $".."
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
buildLevel()
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func buildLevel()->void:
|
||||
print("building level")
|
||||
updateCrates()
|
||||
updateTriggers()
|
||||
|
||||
#wire up signals from GameController
|
||||
game.destroySignal.connect(destroy)
|
||||
game.teleportSignal.connect(teleport)
|
||||
game.levelChangeSignal.connect(loadLevel)
|
||||
|
||||
func updateTriggers()->void:
|
||||
if triggers:
|
||||
for obj in triggers.get_children():
|
||||
if obj is Trigger:
|
||||
if not obj.AreaTrigger.is_connected(game._on_trigger):
|
||||
obj.AreaTrigger.connect(game._on_trigger)
|
||||
|
||||
func updateCrates()->void:
|
||||
#check that there is a crates node
|
||||
if crates:
|
||||
var totalCrates = 0
|
||||
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.crateUpdate(totalCrates)
|
||||
|
||||
func destroy(body)->void:
|
||||
body.queue_free()
|
||||
func teleport(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 loadLevel(level:String)->void:
|
||||
get_tree().call_deferred("change_scene_to_file", level)
|
||||
func makeBullet(targetPosition, speed)->void:
|
||||
print("Make a bullet!")
|
||||
@@ -0,0 +1 @@
|
||||
uid://ch2id76i14srx
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
extends Area2D
|
||||
class_name Trigger extends Area2D
|
||||
|
||||
signal AreaTrigger(body, effect)
|
||||
signal AreaTrigger(body, effect, trigger)
|
||||
@export var effect="destroy"
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
@@ -15,4 +15,4 @@ func _process(delta: float) -> void:
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("Trigger activated")
|
||||
AreaTrigger.emit(body, effect)
|
||||
AreaTrigger.emit(body, effect, self)
|
||||
Reference in new issue
Block a user