bullet, timer, scenemanager keyboard input
This commit is contained in:
parent
76b6721c42
commit
df3ebe62d2
@ -23,6 +23,14 @@ folder_colors={
|
|||||||
"res://scripts/": "green"
|
"res://scripts/": "green"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[input]
|
||||||
|
|
||||||
|
shoot={
|
||||||
|
"deadzone": 0.2,
|
||||||
|
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":90,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
[physics]
|
[physics]
|
||||||
|
|
||||||
3d/physics_engine="Jolt Physics"
|
3d/physics_engine="Jolt Physics"
|
||||||
|
|||||||
13
scenes/bullet.tscn
Normal file
13
scenes/bullet.tscn
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
[gd_scene format=3 uid="uid://ium8i6770pf4"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" uid="uid://bfuhpow7x2xr1" path="res://scripts/bullet.gd" id="1_mkf8s"]
|
||||||
|
|
||||||
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_h1aey"]
|
||||||
|
size = Vector2(12, 6)
|
||||||
|
|
||||||
|
[node name="Bullet" type="Area2D" unique_id=1756277677]
|
||||||
|
script = ExtResource("1_mkf8s")
|
||||||
|
|
||||||
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=646324606]
|
||||||
|
shape = SubResource("RectangleShape2D_h1aey")
|
||||||
|
debug_color = Color(0.93031675, 0.24960315, 0.23292288, 0.41960785)
|
||||||
@ -1,59 +1,71 @@
|
|||||||
[gd_scene format=3 uid="uid://cqk2rgy3m1chs"]
|
[gd_scene format=3 uid="uid://cqk2rgy3m1chs"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://dxubdt7nh5s1j" path="res://scripts/gameController.gd" id="1_lnu2h"]
|
[ext_resource type="Script" uid="uid://dxubdt7nh5s1j" path="res://scripts/gameController.gd" id="1_lnu2h"]
|
||||||
|
[ext_resource type="Script" uid="uid://ch2id76i14srx" path="res://scripts/scene_manager.gd" id="2_iywne"]
|
||||||
[ext_resource type="PackedScene" uid="uid://b6lw2go5mwk3h" path="res://scenes/crate.tscn" id="2_lnu2h"]
|
[ext_resource type="PackedScene" uid="uid://b6lw2go5mwk3h" path="res://scenes/crate.tscn" id="2_lnu2h"]
|
||||||
[ext_resource type="Script" uid="uid://c17de7vvtri1e" path="res://scripts/player.gd" id="2_yqjtg"]
|
[ext_resource type="PackedScene" uid="uid://s0utas3jmhjk" path="res://scenes/player.tscn" id="3_iywne"]
|
||||||
[ext_resource type="PackedScene" uid="uid://bf50mxdma8wq4" path="res://scenes/trigger.tscn" id="4_lbhrr"]
|
[ext_resource type="PackedScene" uid="uid://bf50mxdma8wq4" path="res://scenes/trigger.tscn" id="4_lbhrr"]
|
||||||
|
[ext_resource type="PackedScene" uid="uid://ium8i6770pf4" path="res://scenes/bullet.tscn" id="6_p57ef"]
|
||||||
|
|
||||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
|
||||||
size = Vector2(74, 20)
|
size = Vector2(74, 20)
|
||||||
|
|
||||||
[sub_resource type="CircleShape2D" id="CircleShape2D_uwrxv"]
|
|
||||||
|
|
||||||
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_lbhrr"]
|
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_lbhrr"]
|
||||||
|
|
||||||
[node name="Game" type="Node2D" unique_id=239599901]
|
[node name="Game" type="Node2D" unique_id=239599901]
|
||||||
script = ExtResource("1_lnu2h")
|
script = ExtResource("1_lnu2h")
|
||||||
|
|
||||||
[node name="StaticBody2D" type="StaticBody2D" parent="." unique_id=2062716976]
|
[node name="SceneManager" type="Node2D" parent="." unique_id=195147007]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
script = ExtResource("2_iywne")
|
||||||
|
|
||||||
|
[node name="Player" parent="." unique_id=161143934 instance=ExtResource("3_iywne")]
|
||||||
|
position = Vector2(535, 417)
|
||||||
|
|
||||||
|
[node name="Triggers" type="Node2D" parent="." unique_id=1411399015]
|
||||||
|
|
||||||
|
[node name="Trigger" parent="Triggers" unique_id=1908774248 instance=ExtResource("4_lbhrr")]
|
||||||
|
position = Vector2(692, 509)
|
||||||
|
effect = "destroy"
|
||||||
|
|
||||||
|
[node name="Trigger2" parent="Triggers" unique_id=1627494044 instance=ExtResource("4_lbhrr")]
|
||||||
|
position = Vector2(508, 507)
|
||||||
|
effect = "teleport"
|
||||||
|
|
||||||
|
[node name="Trigger3" parent="Triggers" unique_id=1618797258 instance=ExtResource("4_lbhrr")]
|
||||||
|
position = Vector2(572, 357)
|
||||||
|
effect = "powerup"
|
||||||
|
|
||||||
|
[node name="Crates" type="Node2D" parent="." unique_id=1983554973]
|
||||||
|
|
||||||
|
[node name="Crate" parent="Crates" unique_id=1307809047 instance=ExtResource("2_lnu2h")]
|
||||||
|
position = Vector2(703, 418.00003)
|
||||||
|
|
||||||
|
[node name="Crate2" parent="Crates" unique_id=1988562966 instance=ExtResource("2_lnu2h")]
|
||||||
|
position = Vector2(684, 387)
|
||||||
|
|
||||||
|
[node name="Crate3" parent="Crates" unique_id=1771442258 instance=ExtResource("2_lnu2h")]
|
||||||
|
position = Vector2(575, 255)
|
||||||
|
|
||||||
|
[node name="Crate4" parent="Crates" unique_id=34543093 instance=ExtResource("2_lnu2h")]
|
||||||
|
position = Vector2(694, 351)
|
||||||
|
|
||||||
|
[node name="Level" type="Node2D" parent="." unique_id=1225714734]
|
||||||
|
|
||||||
|
[node name="StaticBody2D" type="StaticBody2D" parent="Level" unique_id=2062716976]
|
||||||
position = Vector2(591, 442)
|
position = Vector2(591, 442)
|
||||||
scale = Vector2(2, 1)
|
scale = Vector2(2, 1)
|
||||||
metadata/_edit_group_ = true
|
metadata/_edit_group_ = true
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D" unique_id=1655047088]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Level/StaticBody2D" unique_id=1655047088]
|
||||||
shape = SubResource("RectangleShape2D_8cj0n")
|
shape = SubResource("RectangleShape2D_8cj0n")
|
||||||
|
|
||||||
[node name="Crate" parent="." unique_id=1307809047 instance=ExtResource("2_lnu2h")]
|
[node name="StaticBody2D2" type="StaticBody2D" parent="Level" unique_id=426786591]
|
||||||
position = Vector2(644, 404)
|
|
||||||
|
|
||||||
[node name="Crate2" parent="." unique_id=1988562966 instance=ExtResource("2_lnu2h")]
|
|
||||||
position = Vector2(609, 403)
|
|
||||||
|
|
||||||
[node name="Player" type="CharacterBody2D" parent="." unique_id=1540403255]
|
|
||||||
position = Vector2(535, 417)
|
|
||||||
script = ExtResource("2_yqjtg")
|
|
||||||
metadata/_edit_group_ = true
|
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Player" unique_id=1365827185]
|
|
||||||
shape = SubResource("CircleShape2D_uwrxv")
|
|
||||||
debug_color = Color(0.28743955, 0.62438726, 0.25921708, 0.41960785)
|
|
||||||
|
|
||||||
[node name="Trigger" parent="." unique_id=1908774248 instance=ExtResource("4_lbhrr")]
|
|
||||||
position = Vector2(692, 509)
|
|
||||||
effect = "destroy"
|
|
||||||
|
|
||||||
[node name="Trigger2" parent="." unique_id=1627494044 instance=ExtResource("4_lbhrr")]
|
|
||||||
position = Vector2(508, 507)
|
|
||||||
|
|
||||||
[node name="StaticBody2D2" type="StaticBody2D" parent="." unique_id=426786591]
|
|
||||||
position = Vector2(601, 622)
|
position = Vector2(601, 622)
|
||||||
metadata/_edit_group_ = true
|
metadata/_edit_group_ = true
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D2" unique_id=895098891]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Level/StaticBody2D2" unique_id=895098891]
|
||||||
shape = SubResource("WorldBoundaryShape2D_lbhrr")
|
shape = SubResource("WorldBoundaryShape2D_lbhrr")
|
||||||
|
|
||||||
[node name="Crate3" parent="." unique_id=1771442258 instance=ExtResource("2_lnu2h")]
|
[node name="Bullet" parent="." unique_id=1756277677 instance=ExtResource("6_p57ef")]
|
||||||
position = Vector2(570, 397)
|
position = Vector2(359, 364)
|
||||||
|
|
||||||
[connection signal="AreaTrigger" from="Trigger" to="." method="_on_trigger"]
|
|
||||||
[connection signal="AreaTrigger" from="Trigger2" to="." method="_on_trigger"]
|
|
||||||
|
|||||||
19
scenes/player.tscn
Normal file
19
scenes/player.tscn
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
[gd_scene format=3 uid="uid://s0utas3jmhjk"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" uid="uid://c17de7vvtri1e" path="res://scripts/player.gd" id="1_3vyb7"]
|
||||||
|
|
||||||
|
[sub_resource type="CircleShape2D" id="CircleShape2D_uwrxv"]
|
||||||
|
|
||||||
|
[node name="Player" type="CharacterBody2D" unique_id=161143934]
|
||||||
|
script = ExtResource("1_3vyb7")
|
||||||
|
metadata/_edit_group_ = true
|
||||||
|
|
||||||
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1128789199]
|
||||||
|
shape = SubResource("CircleShape2D_uwrxv")
|
||||||
|
debug_color = Color(0.28743955, 0.62438726, 0.25921708, 0.41960785)
|
||||||
|
|
||||||
|
[node name="RightTarget" type="Marker2D" parent="." unique_id=1084058194]
|
||||||
|
position = Vector2(15, -3)
|
||||||
|
|
||||||
|
[node name="LeftTarget" type="Marker2D" parent="." unique_id=212282016]
|
||||||
|
position = Vector2(-15, -3)
|
||||||
12
scripts/bullet.gd
Normal file
12
scripts/bullet.gd
Normal file
@ -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
|
||||||
1
scripts/bullet.gd.uid
Normal file
1
scripts/bullet.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://bfuhpow7x2xr1
|
||||||
@ -1 +1,13 @@
|
|||||||
class_name Crate extends RigidBody2D
|
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.
|
# Called when the node enters the scene tree for the first time.
|
||||||
func _ready() -> void:
|
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.
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
func _on_trigger(body: Variant, effect) -> void:
|
func _on_trigger(body: Variant, effect, trigger) -> void:
|
||||||
print("GC knows trigger...."+effect)
|
print("GC knows trigger...."+effect)
|
||||||
if body is Crate:
|
if body is Crate:
|
||||||
match effect:
|
match effect:
|
||||||
@ -20,4 +36,19 @@ func _on_trigger(body: Variant, effect) -> void:
|
|||||||
crateTotal -=1
|
crateTotal -=1
|
||||||
if crateTotal <=0:
|
if crateTotal <=0:
|
||||||
print("You WON!!!")
|
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))
|
||||||
|
|||||||
@ -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 SPEED = 300.0
|
||||||
const JUMP_VELOCITY = -400.0
|
const JUMP_VELOCITY = -400.0
|
||||||
const BUMP_POWER = 50
|
const BUMP_POWER = 50
|
||||||
|
enum FaceDirection{LEFT, RIGHT}
|
||||||
|
var facing:FaceDirection = FaceDirection.RIGHT
|
||||||
|
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
# Add the gravity.
|
# Add the gravity.
|
||||||
if not is_on_floor():
|
if not is_on_floor():
|
||||||
velocity += get_gravity() * delta
|
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.
|
# Handle jump.
|
||||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||||
velocity.y = JUMP_VELOCITY
|
velocity.y = JUMP_VELOCITY
|
||||||
@ -20,6 +34,10 @@ func _physics_process(delta: float) -> void:
|
|||||||
var direction := Input.get_axis("ui_left", "ui_right")
|
var direction := Input.get_axis("ui_left", "ui_right")
|
||||||
if direction:
|
if direction:
|
||||||
velocity.x = direction * SPEED
|
velocity.x = direction * SPEED
|
||||||
|
if direction <0:
|
||||||
|
facing=FaceDirection.LEFT
|
||||||
|
if direction >0:
|
||||||
|
facing=FaceDirection.RIGHT
|
||||||
else:
|
else:
|
||||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||||
|
|
||||||
|
|||||||
58
scripts/scene_manager.gd
Normal file
58
scripts/scene_manager.gd
Normal file
@ -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!")
|
||||||
1
scripts/scene_manager.gd.uid
Normal file
1
scripts/scene_manager.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://ch2id76i14srx
|
||||||
@ -1,6 +1,6 @@
|
|||||||
extends Area2D
|
class_name Trigger extends Area2D
|
||||||
|
|
||||||
signal AreaTrigger(body, effect)
|
signal AreaTrigger(body, effect, trigger)
|
||||||
@export var effect="destroy"
|
@export var effect="destroy"
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# 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:
|
func _on_body_entered(body: Node2D) -> void:
|
||||||
print("Trigger activated")
|
print("Trigger activated")
|
||||||
AreaTrigger.emit(body, effect)
|
AreaTrigger.emit(body, effect, self)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user