initial work on level geometry done from godot 2d nodes
This commit is contained in:
11
Scripts/Main.gd
Normal file
11
Scripts/Main.gd
Normal 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
29
Scripts/Space/Space.gd
Normal 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
|
6
Scripts/Space/SpaceEntity.gd
Normal file
6
Scripts/Space/SpaceEntity.gd
Normal file
@ -0,0 +1,6 @@
|
||||
extends Node2D
|
||||
## Base class for items to be placed in Space.
|
||||
##
|
||||
|
||||
class_name SpaceEntity
|
||||
|
64
Scripts/Space/SpaceRegion.gd
Normal file
64
Scripts/Space/SpaceRegion.gd
Normal 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
|
10
Scripts/Space/SpaceSpawn.gd
Normal file
10
Scripts/Space/SpaceSpawn.gd
Normal 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)
|
Reference in New Issue
Block a user