initial work on level geometry done from godot 2d nodes

This commit is contained in:
veclav talica 2024-08-02 01:29:11 +03:00
commit 88af9b13d6
11 changed files with 230 additions and 0 deletions

18
Scenes/Main.tscn Normal file
View File

@ -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")

7
Scenes/Material.tres Normal file
View File

@ -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

11
Scripts/Main.gd Normal file
View File

@ -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)

29
Scripts/Space/Space.gd Normal file
View File

@ -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

View File

@ -0,0 +1,6 @@
extends Node2D
## Base class for items to be placed in Space.
##
class_name SpaceEntity

View File

@ -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

View File

@ -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)

21
Spaces/Dungeon.tscn Normal file
View File

@ -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")

1
icon.svg Normal file
View File

@ -0,0 +1 @@
<svg height="128" width="128" xmlns="http://www.w3.org/2000/svg"><rect x="2" y="2" width="124" height="124" rx="14" fill="#363d52" stroke="#212532" stroke-width="4"/><g transform="scale(.101) translate(122 122)"><g fill="#fff"><path d="M105 673v33q407 354 814 0v-33z"/><path d="m105 673 152 14q12 1 15 14l4 67 132 10 8-61q2-11 15-15h162q13 4 15 15l8 61 132-10 4-67q3-13 15-14l152-14V427q30-39 56-81-35-59-83-108-43 20-82 47-40-37-88-64 7-51 8-102-59-28-123-42-26 43-46 89-49-7-98 0-20-46-46-89-64 14-123 42 1 51 8 102-48 27-88 64-39-27-82-47-48 49-83 108 26 42 56 81zm0 33v39c0 276 813 276 814 0v-39l-134 12-5 69q-2 10-14 13l-162 11q-12 0-16-11l-10-65H446l-10 65q-4 11-16 11l-162-11q-12-3-14-13l-5-69z" fill="#478cbf"/><path d="M483 600c0 34 58 34 58 0v-86c0-34-58-34-58 0z"/><circle cx="725" cy="526" r="90"/><circle cx="299" cy="526" r="90"/></g><g fill="#414042"><circle cx="307" cy="532" r="60"/><circle cx="717" cy="532" r="60"/></g></g></svg>

After

Width:  |  Height:  |  Size: 949 B

38
icon.svg.import Normal file
View File

@ -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

25
project.godot Normal file
View File

@ -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"