Skip to content

Commit 39732b5

Browse files
authored
Merge pull request #4 from aibrahim185/feat/3d
Feat/3d
2 parents 0e3cfb5 + ce97285 commit 39732b5

File tree

16 files changed

+728
-591
lines changed

16 files changed

+728
-591
lines changed

src/main_scene/main_scene.cpp

Lines changed: 438 additions & 57 deletions
Large diffs are not rendered by default.

src/main_scene/main_scene.hpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
#pragma once
22

3-
#include <godot_cpp/classes/control.hpp>
3+
#include <godot_cpp/classes/node3d.hpp>
44
#include <godot_cpp/classes/button.hpp>
55
#include <godot_cpp/classes/option_button.hpp>
66
#include <godot_cpp/classes/label.hpp>
77
#include <godot_cpp/classes/file_dialog.hpp>
8+
#include <godot_cpp/classes/packed_scene.hpp>
9+
#include <godot_cpp/classes/standard_material3d.hpp>
10+
#include <godot_cpp/classes/shader_material.hpp>
11+
#include <godot_cpp/classes/resource_loader.hpp>
12+
#include <godot_cpp/classes/mesh_instance3d.hpp>
13+
#include <godot_cpp/classes/csg_primitive3d.hpp>
14+
#include <godot_cpp/classes/plane_mesh.hpp>
15+
#include <godot_cpp/classes/collision_shape3d.hpp>
16+
#include <godot_cpp/classes/concave_polygon_shape3d.hpp>
17+
#include <godot_cpp/classes/scene_tree.hpp>
18+
#include <godot_cpp/classes/tween.hpp>
19+
#include <godot_cpp/classes/property_tweener.hpp>
20+
#include <godot_cpp/classes/engine.hpp>
821

922
#include <godot_cpp/variant/utility_functions.hpp>
1023

1124
#include <fstream>
1225
#include <vector>
26+
#include <map>
1327

1428
#include "../utils/utils.hpp"
1529

1630
using namespace godot;
1731
using namespace std;
1832

19-
class MainScene : public Control {
20-
GDCLASS(MainScene, Control)
33+
class MainScene : public Node3D {
34+
GDCLASS(MainScene, Node3D)
2135

2236
private:
2337
vector<Piece> pieces;
@@ -28,13 +42,32 @@ class MainScene : public Control {
2842
bool is_solved = false;
2943
AlgoType algo_type = BFS;
3044

31-
Label* time = nullptr;
45+
Label* time_label = nullptr;
3246

3347
Button* solve_button = nullptr;
3448
Button* reset_button = nullptr;
3549
Button* load_button = nullptr;
3650
OptionButton* algo_button = nullptr;
3751

52+
Ref<PackedScene> car2_template;
53+
Ref<PackedScene> car3_template;
54+
Ref<StandardMaterial3D> primary_piece_material;
55+
Ref<StandardMaterial3D> non_primary_piece_material;
56+
MeshInstance3D* floor = nullptr;
57+
vector<Node3D*> spawned_car_nodes;
58+
Vector3 floor_center_offset = Vector3(0.0f, 0.0f, 0.0f);
59+
60+
map<Coordinates, Node3D*> coord_to_car_node_map;
61+
62+
Solution current_solution;
63+
int current_move_index = -1;
64+
bool is_animating_solution = false;
65+
const float ANIMATION_DURATION = 0.5f;
66+
67+
void _animate_next_move();
68+
void _on_move_animation_finished();
69+
Vector3 _get_3d_position_for_piece_coords(const Coordinates& piece_top_left_coords, int piece_size, bool is_vertical_piece);
70+
3871
void _on_solve_button_pressed();
3972
void _on_reset_button_pressed();
4073
void _on_algo_button_selected(int index);
@@ -46,6 +79,9 @@ class MainScene : public Control {
4679

4780
bool load_input(String path, vector<Piece>& pieces, Board& board);
4881

82+
void _clear_all_cars();
83+
void _spawn_piece_as_car(const Piece& piece_data);
84+
4985
protected:
5086
static void _bind_methods();
5187
void _notification(int p_what);

src/utils/utils.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ enum AlgoType {
2323
struct Coordinates {
2424
int x;
2525
int y;
26+
27+
bool operator<(const Coordinates& other) const {
28+
if (x != other.x) {
29+
return x < other.x;
30+
}
31+
return y < other.y;
32+
}
33+
34+
bool operator==(const Coordinates& other) const {
35+
return x == other.x && y == other.y;
36+
}
37+
38+
bool operator!=(const Coordinates& other) const {
39+
return !(*this == other);
40+
}
2641
};
2742

2843
struct Piece {

test/2.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
5 5
2+
5
3+
EAAA.
4+
ED.PB
5+
.D.PB
6+
.DCCB
7+
.....
8+
K
-84.8 KB
Binary file not shown.

tukang-parkir-simulator/assets/Textures-16.png.import

Lines changed: 0 additions & 34 deletions
This file was deleted.

tukang-parkir-simulator/project.godot

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ config_version=5
1111
[application]
1212

1313
config/name="tukang_parkir_simulator"
14-
run/main_scene="res://scenes/main.tscn"
14+
run/main_scene="uid://deic8j1v28f5m"
1515
config/features=PackedStringArray("4.4", "Forward Plus")
1616
config/icon="res://icon.svg"
1717

@@ -21,6 +21,10 @@ window/size/viewport_width=1280
2121
window/size/viewport_height=720
2222
window/stretch/mode="viewport"
2323

24+
[physics]
25+
26+
3d/physics_engine="Jolt Physics"
27+
2428
[rendering]
2529

2630
renderer/rendering_method="gl_compatibility"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[gd_scene load_steps=4 format=3 uid="uid://bj1kkdl8im8kb"]
2+
3+
[ext_resource type="Material" uid="uid://6hu2nb8ajcvx" path="res://scenes/non_primary_material.tres" id="1_2fv4s"]
4+
[ext_resource type="Material" uid="uid://dyl801edovtaw" path="res://scenes/wheel_material.tres" id="1_je4r3"]
5+
6+
[sub_resource type="BoxShape3D" id="BoxShape3D_rc6gd"]
7+
size = Vector3(0.8, 0.8, 1.8)
8+
9+
[node name="Car2" type="RigidBody3D"]
10+
transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0.9, 0, 0.4)
11+
12+
[node name="Body" type="CSGBox3D" parent="."]
13+
size = Vector3(0.7, 0.5, 1.8)
14+
material = ExtResource("1_2fv4s")
15+
16+
[node name="Wheel" type="CSGCylinder3D" parent="Body"]
17+
transform = Transform3D(-4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0, 1, -0.3, -0.2, -0.5)
18+
radius = 0.2
19+
height = 0.2
20+
material = ExtResource("1_je4r3")
21+
22+
[node name="Wheel2" type="CSGCylinder3D" parent="Body"]
23+
transform = Transform3D(1.31134e-07, 1, 0, -1, 1.31134e-07, 0, 0, 0, 1, 0.3, -0.2, -0.5)
24+
radius = 0.2
25+
height = 0.2
26+
material = ExtResource("1_je4r3")
27+
28+
[node name="Wheel3" type="CSGCylinder3D" parent="Body"]
29+
transform = Transform3D(-4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0, 1, -0.3, -0.2, 0.5)
30+
radius = 0.2
31+
height = 0.2
32+
material = ExtResource("1_je4r3")
33+
34+
[node name="Wheel4" type="CSGCylinder3D" parent="Body"]
35+
transform = Transform3D(-4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0, 1, 0.3, -0.2, 0.5)
36+
radius = 0.2
37+
height = 0.2
38+
material = ExtResource("1_je4r3")
39+
40+
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
41+
shape = SubResource("BoxShape3D_rc6gd")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[gd_scene load_steps=4 format=3 uid="uid://nk8hrlhom63t"]
2+
3+
[ext_resource type="Material" uid="uid://c0xrqwe2nwfyy" path="res://scenes/primary_material.tres" id="1_1b0sv"]
4+
[ext_resource type="Material" uid="uid://dyl801edovtaw" path="res://scenes/wheel_material.tres" id="1_6a10o"]
5+
6+
[sub_resource type="BoxShape3D" id="BoxShape3D_hnjk1"]
7+
size = Vector3(0.8, 0.8, 2.8)
8+
9+
[node name="Car3" type="RigidBody3D"]
10+
transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 1.4, 0, 0.4)
11+
12+
[node name="Body" type="CSGBox3D" parent="."]
13+
size = Vector3(0.7, 0.6, 2.8)
14+
material = ExtResource("1_1b0sv")
15+
16+
[node name="Wheel" type="CSGCylinder3D" parent="Body"]
17+
transform = Transform3D(-4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0, 1, -0.3, -0.2, -0.9)
18+
radius = 0.2
19+
height = 0.2
20+
material = ExtResource("1_6a10o")
21+
22+
[node name="Wheel2" type="CSGCylinder3D" parent="Body"]
23+
transform = Transform3D(1.31134e-07, 1, 0, -1, 1.31134e-07, 0, 0, 0, 1, 0.3, -0.2, -0.9)
24+
radius = 0.2
25+
height = 0.2
26+
material = ExtResource("1_6a10o")
27+
28+
[node name="Wheel3" type="CSGCylinder3D" parent="Body"]
29+
transform = Transform3D(-4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0, 1, -0.3, -0.2, 0.8)
30+
radius = 0.2
31+
height = 0.2
32+
material = ExtResource("1_6a10o")
33+
34+
[node name="Wheel4" type="CSGCylinder3D" parent="Body"]
35+
transform = Transform3D(-4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0, 1, 0.3, -0.2, 0.8)
36+
radius = 0.2
37+
height = 0.2
38+
material = ExtResource("1_6a10o")
39+
40+
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
41+
shape = SubResource("BoxShape3D_hnjk1")
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
shader_type spatial;
2+
3+
uniform vec4 base_color : source_color = vec4(0.8, 0.8, 0.8, 1.0);
4+
uniform vec4 line_color : source_color = vec4(0.0, 0.0, 0.0, 1.0);
5+
uniform float grid_spacing = 1.0;
6+
uniform float line_thickness = 0.1;
7+
uniform vec2 mesh_actual_size;
8+
uniform int grid_plane = 1;
9+
10+
varying vec3 v_local_position;
11+
12+
void vertex() {
13+
v_local_position = VERTEX;
14+
}
15+
16+
void fragment() {
17+
vec2 coord_on_plane;
18+
19+
if (grid_plane == 1) {
20+
coord_on_plane = v_local_position.xz;
21+
} else if (grid_plane == 0) {
22+
coord_on_plane = v_local_position.xy;
23+
} else {
24+
coord_on_plane = v_local_position.yz;
25+
}
26+
27+
vec2 shifted_coord = coord_on_plane + mesh_actual_size / 2.0;
28+
float half_thick = line_thickness / 2.0;
29+
30+
float mod_val_x = mod(shifted_coord.x, grid_spacing);
31+
bool on_vertical_line = (mod_val_x < half_thick || mod_val_x > grid_spacing - half_thick);
32+
33+
if (abs(shifted_coord.x - mesh_actual_size.x) < half_thick && mesh_actual_size.x > 0.001) {
34+
on_vertical_line = true;
35+
}
36+
37+
float mod_val_y = mod(shifted_coord.y, grid_spacing);
38+
bool on_horizontal_line = (mod_val_y < half_thick || mod_val_y > grid_spacing - half_thick);
39+
40+
if (abs(shifted_coord.y - mesh_actual_size.y) < half_thick && mesh_actual_size.y > 0.001) {
41+
on_horizontal_line = true;
42+
}
43+
44+
if (on_vertical_line || on_horizontal_line) {
45+
ALBEDO = line_color.rgb;
46+
ALPHA = line_color.a;
47+
} else {
48+
ALBEDO = base_color.rgb;
49+
ALPHA = base_color.a;
50+
}
51+
}

0 commit comments

Comments
 (0)