scenemanager, raycasts, shove attack
This commit is contained in:
parent
60729215f2
commit
7384736795
13
Scenes/crate.tscn
Normal file
13
Scenes/crate.tscn
Normal file
@ -0,0 +1,13 @@
|
||||
[gd_scene format=3 uid="uid://d16epxgmccvkp"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dayw2gghh8mym" path="res://Scripts/crate.gd" id="1_wp72f"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ebmjs"]
|
||||
|
||||
[node name="Crate" type="RigidBody2D" unique_id=1338541987 groups=["destructible"]]
|
||||
script = ExtResource("1_wp72f")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1809235182]
|
||||
shape = SubResource("RectangleShape2D_ebmjs")
|
||||
debug_color = Color(0.8535397, 0.36730364, 0.11225829, 0.41960785)
|
||||
@ -1,24 +1,26 @@
|
||||
[gd_scene format=3 uid="uid://b06dlp8n6upld"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://btmoyp0rwqmxe" path="res://Scripts/player.gd" id="1_ebmjs"]
|
||||
[ext_resource type="Script" uid="uid://bc25u4w35ibeu" path="res://Scripts/gameController.gd" id="1_qxrlw"]
|
||||
[ext_resource type="PackedScene" uid="uid://d16epxgmccvkp" path="res://Scenes/crate.tscn" id="2_3dryh"]
|
||||
[ext_resource type="Script" uid="uid://cnm7tj6umtwtm" path="res://Scripts/scene_manager.gd" id="2_wowpa"]
|
||||
[ext_resource type="PackedScene" uid="uid://caa73phf3ng5m" path="res://Scenes/player.tscn" id="3_wowpa"]
|
||||
[ext_resource type="Script" uid="uid://4hekg0d8n04f" path="res://Scripts/trigger.gd" id="3_wrm1d"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_2poj3"]
|
||||
size = Vector2(84, 20)
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ebmjs"]
|
||||
|
||||
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_2poj3"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_2poj3"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_3dryh"]
|
||||
radius = 32.01562
|
||||
|
||||
[node name="Game" type="Node2D" unique_id=1698001017]
|
||||
script = ExtResource("1_qxrlw")
|
||||
|
||||
[node name="SceneManager" type="Node2D" parent="." unique_id=1394046756]
|
||||
unique_name_in_owner = true
|
||||
script = ExtResource("2_wowpa")
|
||||
|
||||
[node name="platform" type="StaticBody2D" parent="." unique_id=601020872]
|
||||
position = Vector2(436, 297)
|
||||
constant_linear_velocity = Vector2(10, 0)
|
||||
@ -27,24 +29,6 @@ metadata/_edit_group_ = true
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="platform" unique_id=1836025932]
|
||||
shape = SubResource("RectangleShape2D_2poj3")
|
||||
|
||||
[node name="RigidBody2D" type="RigidBody2D" parent="." unique_id=2015643131]
|
||||
position = Vector2(415.00003, 189)
|
||||
rotation = 0.46453398
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D" unique_id=1377201570]
|
||||
shape = SubResource("RectangleShape2D_ebmjs")
|
||||
debug_color = Color(0.8535397, 0.36730364, 0.11225829, 0.41960785)
|
||||
|
||||
[node name="RigidBody2D2" type="RigidBody2D" parent="." unique_id=800232702]
|
||||
position = Vector2(443, 191.00003)
|
||||
rotation = 0.9293104
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D2" unique_id=2111607142]
|
||||
shape = SubResource("RectangleShape2D_ebmjs")
|
||||
debug_color = Color(0.8535397, 0.36730364, 0.11225829, 0.41960785)
|
||||
|
||||
[node name="StaticBody2D" type="StaticBody2D" parent="." unique_id=1600506296]
|
||||
position = Vector2(448, 452)
|
||||
metadata/_edit_group_ = true
|
||||
@ -52,14 +36,8 @@ metadata/_edit_group_ = true
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D" unique_id=1926005878]
|
||||
shape = SubResource("WorldBoundaryShape2D_2poj3")
|
||||
|
||||
[node name="CharacterBody2D" type="CharacterBody2D" parent="." unique_id=380327915]
|
||||
position = Vector2(437, 250)
|
||||
script = ExtResource("1_ebmjs")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D" unique_id=1235513256]
|
||||
shape = SubResource("CircleShape2D_2poj3")
|
||||
debug_color = Color(0.36777267, 0.6132997, 0.17400515, 0.41960785)
|
||||
[node name="Player" parent="." unique_id=1717313761 instance=ExtResource("3_wowpa")]
|
||||
position = Vector2(338, 430)
|
||||
|
||||
[node name="Area2D" type="Area2D" parent="." unique_id=1184709580]
|
||||
position = Vector2(495, 356)
|
||||
@ -70,5 +48,16 @@ metadata/_edit_group_ = true
|
||||
shape = SubResource("CircleShape2D_3dryh")
|
||||
debug_color = Color(0.7716697, 0.28327075, 0.8513461, 0.41960785)
|
||||
|
||||
[node name="Crates" type="Node2D" parent="." unique_id=1980240897]
|
||||
|
||||
[node name="Crate" parent="Crates" unique_id=1338541987 instance=ExtResource("2_3dryh")]
|
||||
position = Vector2(385, 428)
|
||||
|
||||
[node name="Crate2" parent="Crates" unique_id=1931037062 instance=ExtResource("2_3dryh")]
|
||||
position = Vector2(430, 140)
|
||||
|
||||
[node name="Crate4" parent="Crates" unique_id=585069069 instance=ExtResource("2_3dryh")]
|
||||
position = Vector2(461, 134)
|
||||
|
||||
[connection signal="body_entered" from="Area2D" to="Area2D" method="_on_body_entered"]
|
||||
[connection signal="triggerActiveSignal" from="Area2D" to="." method="_on_triggerSignal"]
|
||||
|
||||
19
Scenes/player.tscn
Normal file
19
Scenes/player.tscn
Normal file
@ -0,0 +1,19 @@
|
||||
[gd_scene format=3 uid="uid://caa73phf3ng5m"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://btmoyp0rwqmxe" path="res://Scripts/player.gd" id="1_v0iea"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_2poj3"]
|
||||
|
||||
[node name="Player" type="CharacterBody2D" unique_id=1717313761]
|
||||
script = ExtResource("1_v0iea")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=528481441]
|
||||
shape = SubResource("CircleShape2D_2poj3")
|
||||
debug_color = Color(0.36777267, 0.6132997, 0.17400515, 0.41960785)
|
||||
|
||||
[node name="RightCast" type="RayCast2D" parent="." unique_id=1332937468]
|
||||
target_position = Vector2(17, 0)
|
||||
|
||||
[node name="LeftCast" type="RayCast2D" parent="." unique_id=839132816]
|
||||
target_position = Vector2(-17, 0)
|
||||
1
Scripts/crate.gd
Normal file
1
Scripts/crate.gd
Normal file
@ -0,0 +1 @@
|
||||
class_name Crate extends RigidBody2D
|
||||
1
Scripts/crate.gd.uid
Normal file
1
Scripts/crate.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://dayw2gghh8mym
|
||||
@ -1,5 +1,6 @@
|
||||
extends Node2D
|
||||
class_name GameController extends Node2D
|
||||
|
||||
signal destroySignal(body)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
@ -13,5 +14,10 @@ func _process(delta: float) -> void:
|
||||
|
||||
func _on_triggerSignal(body: Variant, intentMessage: Variant) -> void:
|
||||
print("GC knows about a trigger!")
|
||||
if not body is Player and intentMessage == "destroy":
|
||||
body.queue_free()
|
||||
if body.is_in_group("destructible") and intentMessage == "destroy":
|
||||
destroySignal.emit(body)
|
||||
|
||||
func totalCrates(numberOfCrates:int)->void:
|
||||
print("GC knows total crates: "+str(numberOfCrates))
|
||||
if numberOfCrates <=0:
|
||||
print("You WON!!!!")
|
||||
|
||||
@ -1,27 +1,73 @@
|
||||
class_name Player extends CharacterBody2D
|
||||
@onready var right_cast: RayCast2D = $RightCast
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
var direction
|
||||
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.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_input()->void:
|
||||
if Input.is_action_just_pressed("shove") and pushEnabled:
|
||||
print("Shove somethin")
|
||||
# determine what direction to shove the thing
|
||||
var shoveDirection:int
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
shoveDirection = 1
|
||||
FaceDirection.LEFT:
|
||||
shoveDirection = -1
|
||||
pushTarget.apply_central_impulse(Vector2(shoveDirection,0)*700)
|
||||
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# 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")
|
||||
direction = Input.get_axis("left", "right")
|
||||
if direction<0:
|
||||
facing = FaceDirection.LEFT
|
||||
if direction>0:
|
||||
facing = FaceDirection.RIGHT
|
||||
|
||||
func handle_movement(delta)->void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
# Handle jump.
|
||||
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
func handle_collisions()->void:
|
||||
if right_cast.is_colliding() and facing==FaceDirection.RIGHT:
|
||||
print("Right raycast contact!!")
|
||||
pushEnabled = true
|
||||
var collider = right_cast.get_collider()
|
||||
pushTarget = collider
|
||||
|
||||
if left_cast.is_colliding() and facing==FaceDirection.LEFT:
|
||||
print("Left raycast contact!!")
|
||||
pushEnabled = true
|
||||
var collider = left_cast.get_collider()
|
||||
pushTarget = collider
|
||||
|
||||
if not right_cast.is_colliding() and not left_cast.is_colliding():
|
||||
pushEnabled = false
|
||||
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
|
||||
27
Scripts/scene_manager.gd
Normal file
27
Scripts/scene_manager.gd
Normal file
@ -0,0 +1,27 @@
|
||||
class_name SceneManager extends Node2D
|
||||
@onready var game: Node2D = $".."
|
||||
@onready var crates: Node2D = $"../Crates"
|
||||
|
||||
func _ready() -> void:
|
||||
print("Scene manager is ready!")
|
||||
game.destroySignal.connect(destroy)
|
||||
buildLevel()
|
||||
|
||||
func destroy(body)->void:
|
||||
if body is Crate:
|
||||
body.queue_free()
|
||||
|
||||
func buildLevel()->void:
|
||||
updateCrates()
|
||||
|
||||
func updateCrates()->void:
|
||||
#is there a crate holder?
|
||||
var _crateTotal:int = 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)
|
||||
_crateTotal +=1
|
||||
print("Number of crates: "+str(_crateTotal))
|
||||
game.totalCrates(_crateTotal)
|
||||
1
Scripts/scene_manager.gd.uid
Normal file
1
Scripts/scene_manager.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cnm7tj6umtwtm
|
||||
@ -21,6 +21,30 @@ folder_colors={
|
||||
"res://Scripts/": "red"
|
||||
}
|
||||
|
||||
[input]
|
||||
|
||||
shove={
|
||||
"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":74,"key_label":0,"unicode":106,"location":0,"echo":false,"script":null)
|
||||
, 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":85,"key_label":0,"unicode":117,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
jump={
|
||||
"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":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
left={
|
||||
"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":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
right={
|
||||
"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":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
||||
[physics]
|
||||
|
||||
3d/physics_engine="Jolt Physics"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user