From 88af9b13d62364c456922af009590048b857acef Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Fri, 2 Aug 2024 01:29:11 +0300 Subject: [PATCH] initial work on level geometry done from godot 2d nodes --- Scenes/Main.tscn | 18 ++++++++++ Scenes/Material.tres | 7 ++++ Scripts/Main.gd | 11 +++++++ Scripts/Space/Space.gd | 29 ++++++++++++++++ Scripts/Space/SpaceEntity.gd | 6 ++++ Scripts/Space/SpaceRegion.gd | 64 ++++++++++++++++++++++++++++++++++++ Scripts/Space/SpaceSpawn.gd | 10 ++++++ Spaces/Dungeon.tscn | 21 ++++++++++++ icon.svg | 1 + icon.svg.import | 38 +++++++++++++++++++++ project.godot | 25 ++++++++++++++ 11 files changed, 230 insertions(+) create mode 100644 Scenes/Main.tscn create mode 100644 Scenes/Material.tres create mode 100644 Scripts/Main.gd create mode 100644 Scripts/Space/Space.gd create mode 100644 Scripts/Space/SpaceEntity.gd create mode 100644 Scripts/Space/SpaceRegion.gd create mode 100644 Scripts/Space/SpaceSpawn.gd create mode 100644 Spaces/Dungeon.tscn create mode 100644 icon.svg create mode 100644 icon.svg.import create mode 100644 project.godot diff --git a/Scenes/Main.tscn b/Scenes/Main.tscn new file mode 100644 index 0000000..890c256 --- /dev/null +++ b/Scenes/Main.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=4 format=3 uid="uid://i5jawm0cwlat"] + +[ext_resource type="Script" path="res://Scripts/Main.gd" id="1_2yjjx"] +[ext_resource type="PackedScene" uid="uid://deff7lt34nj0h" path="res://Spaces/Dungeon.tscn" id="2_4gs22"] + +[sub_resource type="Environment" id="Environment_pn2uj"] + +[node name="Main" type="Node3D"] +script = ExtResource("1_2yjjx") + +[node name="Dungeon" parent="." instance=ExtResource("2_4gs22")] +visible = false + +[node name="Camera" type="Camera3D" parent="."] +transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0) + +[node name="WorldEnvironment" type="WorldEnvironment" parent="."] +environment = SubResource("Environment_pn2uj") diff --git a/Scenes/Material.tres b/Scenes/Material.tres new file mode 100644 index 0000000..a41bb13 --- /dev/null +++ b/Scenes/Material.tres @@ -0,0 +1,7 @@ +[gd_resource type="StandardMaterial3D" load_steps=2 format=3 uid="uid://bb47mhgbxy3cw"] + +[ext_resource type="Texture2D" uid="uid://h0skytm8grcu" path="res://icon.svg" id="1_2jq3x"] + +[resource] +albedo_texture = ExtResource("1_2jq3x") +texture_filter = 1 diff --git a/Scripts/Main.gd b/Scripts/Main.gd new file mode 100644 index 0000000..57f250a --- /dev/null +++ b/Scripts/Main.gd @@ -0,0 +1,11 @@ +extends Node3D + + +func _ready(): + add_child($Dungeon.generate_geometry()) + $Camera.position = $Dungeon.get_spawn_point() + $Camera.position.y += 0.5 + + +func _process(delta): + $Camera.rotate_y(0.01) diff --git a/Scripts/Space/Space.gd b/Scripts/Space/Space.gd new file mode 100644 index 0000000..8ab65c2 --- /dev/null +++ b/Scripts/Space/Space.gd @@ -0,0 +1,29 @@ +extends Node2D +## Scene based space to transform into 3d. +## +## Children of class SpaceEntity will be interpreted. +## + +class_name Space + +## Scaling of pixel to 3d space coordinates. +@export var unit_scale: float = 0.01 + + +func generate_geometry() -> Node3D: + var root := Node3D.new() + + for child in get_children(): + if child is SpaceRegion: + var geometry = child.generate_geometry(self) + root.add_child(geometry) + + return root + + +func get_spawn_point() -> Vector3: + for child in get_children(): + if child is SpaceSpawn: + return child.space_position(self) + + return Vector3.ZERO diff --git a/Scripts/Space/SpaceEntity.gd b/Scripts/Space/SpaceEntity.gd new file mode 100644 index 0000000..5be48e3 --- /dev/null +++ b/Scripts/Space/SpaceEntity.gd @@ -0,0 +1,6 @@ +extends Node2D +## Base class for items to be placed in Space. +## + +class_name SpaceEntity + diff --git a/Scripts/Space/SpaceRegion.gd b/Scripts/Space/SpaceRegion.gd new file mode 100644 index 0000000..d8b2cbb --- /dev/null +++ b/Scripts/Space/SpaceRegion.gd @@ -0,0 +1,64 @@ +extends SpaceEntity + +class_name SpaceRegion + + +@export var elevation: float = 0.0 +@export var height: float = 100.0 + + +func generate_geometry(space: Space) -> Node3D: + var geometry := MeshInstance3D.new() + geometry.name = name + + var polygon = self.polygon + var polygon_pairs = Array() + for i in range(-1, polygon.size() - 1): + polygon_pairs.append([polygon[i], polygon[i+1]]) + + var vertices = PackedVector3Array() + var uvs = PackedVector2Array() + for line in polygon_pairs: + vertices.append(Vector3( + line[0][0] * space.unit_scale, elevation * space.unit_scale, + line[0][1] * space.unit_scale + )) + vertices.append(Vector3( + line[1][0] * space.unit_scale, (elevation + height) * space.unit_scale, + line[1][1] * space.unit_scale, + )) + vertices.append(Vector3( + line[1][0] * space.unit_scale, elevation * space.unit_scale, + line[1][1] * space.unit_scale, + )) + vertices.append(Vector3( + line[1][0] * space.unit_scale, (elevation + height) * space.unit_scale, + line[1][1] * space.unit_scale, + )) + vertices.append(Vector3( + line[0][0] * space.unit_scale, elevation * space.unit_scale, + line[0][1] * space.unit_scale, + )) + vertices.append(Vector3( + line[0][0] * space.unit_scale, (elevation + height) * space.unit_scale, + line[0][1] * space.unit_scale, + )) + uvs.append(Vector2(0, 1)) + uvs.append(Vector2(1, 0)) + uvs.append(Vector2(1, 1)) + uvs.append(Vector2(1, 0)) + uvs.append(Vector2(0, 1)) + uvs.append(Vector2(0, 0)) + + var arrays = Array() + arrays.resize(Mesh.ARRAY_MAX) + arrays[Mesh.ARRAY_VERTEX] = vertices + arrays[Mesh.ARRAY_TEX_UV] = uvs + + var mesh = ArrayMesh.new() + mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays) + mesh.surface_set_material(0, preload("res://Scenes/Material.tres")) + + geometry.mesh = mesh + + return geometry diff --git a/Scripts/Space/SpaceSpawn.gd b/Scripts/Space/SpaceSpawn.gd new file mode 100644 index 0000000..0b180b7 --- /dev/null +++ b/Scripts/Space/SpaceSpawn.gd @@ -0,0 +1,10 @@ +extends SpaceEntity +## Position for mob to be spawned in. +## + +class_name SpaceSpawn + + +func space_position(space: Space) -> Vector3: + # todo: Get current region elevation + return Vector3(position.x * space.unit_scale, 0, position.y * space.unit_scale) diff --git a/Spaces/Dungeon.tscn b/Spaces/Dungeon.tscn new file mode 100644 index 0000000..2bd43d9 --- /dev/null +++ b/Spaces/Dungeon.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=5 format=3 uid="uid://deff7lt34nj0h"] + +[ext_resource type="Script" path="res://Scripts/Space/Space.gd" id="1_ckp7u"] +[ext_resource type="Script" path="res://Scripts/Space/SpaceRegion.gd" id="2_s3h7s"] +[ext_resource type="Texture2D" uid="uid://h0skytm8grcu" path="res://icon.svg" id="3_7vff1"] +[ext_resource type="Script" path="res://Scripts/Space/SpaceSpawn.gd" id="4_rhl23"] + +[node name="Dungeon" type="Node2D"] +script = ExtResource("1_ckp7u") + +[node name="Room" type="Polygon2D" parent="."] +position = Vector2(-30, -8) +polygon = PackedVector2Array(30, 72, 262, 32, 497, 192, 286, 328, 94, 264) +script = ExtResource("2_s3h7s") + +[node name="Spawn" type="Sprite2D" parent="."] +position = Vector2(72, 104) +rotation = 0.159622 +scale = Vector2(0.25, 0.25) +texture = ExtResource("3_7vff1") +script = ExtResource("4_rhl23") diff --git a/icon.svg b/icon.svg new file mode 100644 index 0000000..3fe4f4a --- /dev/null +++ b/icon.svg @@ -0,0 +1 @@ + diff --git a/icon.svg.import b/icon.svg.import new file mode 100644 index 0000000..c7136c7 --- /dev/null +++ b/icon.svg.import @@ -0,0 +1,38 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://h0skytm8grcu" +path.s3tc="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.s3tc.ctex" +metadata={ +"imported_formats": ["s3tc_bptc"], +"vram_texture": true +} + +[deps] + +source_file="res://icon.svg" +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.s3tc.ctex"] + +[params] + +compress/mode=2 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=true +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=0 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/project.godot b/project.godot new file mode 100644 index 0000000..ef7dd37 --- /dev/null +++ b/project.godot @@ -0,0 +1,25 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="Salesman" +run/main_scene="res://Scenes/Main.tscn" +config/features=PackedStringArray("4.2", "GL Compatibility") +config/icon="res://icon.svg" + +[editor_plugins] + +enabled=PackedStringArray() + +[rendering] + +renderer/rendering_method="gl_compatibility" +renderer/rendering_method.mobile="gl_compatibility"