Browse Source

Can create a level programmatically

master
killruana 11 months ago
parent
commit
a54177f828
5 changed files with 100 additions and 28 deletions
  1. +0
    -24
      objects/level_1.tscn
  2. +6
    -0
      project.godot
  3. +3
    -4
      scenes/game.tscn
  4. +23
    -0
      scripts/game.gd
  5. +68
    -0
      scripts/level.gd

+ 0
- 24
objects/level_1.tscn View File

@@ -1,24 +0,0 @@
[gd_scene load_steps=4 format=2]

[ext_resource path="res://materials/floor.material" type="Material" id=1]
[ext_resource path="res://meshes/mesh_library.meshlib" type="MeshLibrary" id=2]

[sub_resource type="PlaneMesh" id=1]
size = Vector2( 22, 22 )

[node name="root" type="Node"]

[node name="ground" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 11, 0, -11 )
mesh = SubResource( 1 )
material/0 = ExtResource( 1 )

[node name="walls" type="GridMap" parent="."]
mesh_library = ExtResource( 2 )
data = {
"cells": PoolIntArray( 0, 65525, 0, 1, 65525, 0, 2, 65525, 0, 3, 65525, 0, 4, 65525, 0, 5, 65525, 0, 6, 65525, -1610612736, 7, 65525, 0, 8, 65525, 0, 9, 65525, 0, 10, 65525, 0, 0, 65526, -1610612736, 10, 65526, 0, 0, 65527, 0, 2, 65527, 0, 3, 65527, 0, 4, 65527, 0, 6, 65527, 0, 7, 65527, 0, 8, 65527, 0, 10, 65527, -2147483648, 0, 65528, 0, 2, 65528, 0, 8, 65528, 1073741824, 10, 65528, 536870912, 0, 65529, 0, 2, 65529, 0, 5, 65529, 1073741824, 8, 65529, 0, 10, 65529, 0, 0, 65530, 0, 4, 65530, 0, 5, 65530, 0, 6, 65530, 0, 10, 65530, 0, 0, 65531, 0, 2, 65531, 0, 5, 65531, 0, 8, 65531, 0, 10, 65531, 0, 0, 65532, 0, 2, 65532, 0, 8, 65532, 0, 10, 65532, 0, 0, 65533, 0, 2, 65533, 0, 3, 65533, 0, 4, 65533, 0, 6, 65533, 0, 7, 65533, 0, 8, 65533, 0, 10, 65533, 0, 0, 65534, 0, 10, 65534, -2147483648, 0, 65535, 0, 1, 65535, 0, 2, 65535, 0, 3, 65535, 0, 4, 65535, 0, 5, 65535, 0, 6, 65535, 0, 7, 65535, 0, 8, 65535, 0, 9, 65535, 0, 10, 65535, 0 )
}
__meta__ = {
"_editor_clip_": 0
}


+ 6
- 0
project.godot View File

@@ -9,12 +9,18 @@
config_version=4

_global_script_classes=[ {
"base": "Reference",
"class": "Level",
"language": "GDScript",
"path": "res://scripts/level.gd"
}, {
"base": "KinematicBody",
"class": "Player",
"language": "GDScript",
"path": "res://scripts/player.gd"
} ]
_global_script_class_icons={
"Level": "",
"Player": ""
}



+ 3
- 4
scenes/game.tscn View File

@@ -1,17 +1,16 @@
[gd_scene load_steps=4 format=2]

[ext_resource path="res://objects/level_1.tscn" type="PackedScene" id=1]
[ext_resource path="res://scripts/game.gd" type="Script" id=1]
[ext_resource path="res://scripts/player.gd" type="Script" id=2]

[sub_resource type="SphereShape" id=1]
radius = 0.5

[node name="root" type="Node"]

[node name="level" parent="." instance=ExtResource( 1 )]
script = ExtResource( 1 )

[node name="player" type="KinematicBody" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 3, 1, -3 )
transform = Transform( -1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 3, 1, 3 )
script = ExtResource( 2 )

[node name="CollisionShape" type="CollisionShape" parent="player"]


+ 23
- 0
scripts/game.gd View File

@@ -0,0 +1,23 @@
extends Node

func _ready():
var level_data : String = (
"###########"
+ "# #"
+ "# ### ### #"
+ "# # # #"
+ "# # # # #"
+ "# ### #"
+ "# # # # #"
+ "# # # #"
+ "# ### ### #"
+ "# #"
+ "###########"
)

var level : Level = Level.new(11, 11, level_data)
var level_instance : Node = level.instance()
self.add_child(level_instance)

var player : Node = self.get_node("./player")
player.translation = level.get_start_position()

+ 68
- 0
scripts/level.gd View File

@@ -0,0 +1,68 @@
class_name Level

enum CellType {
Clear,
Wall
}

var width : int
var height : int
var cells : Array
var start_position : Vector2
const CELL_SIZE : int = 2

func _init(width : int, height : int, data : String, start_position : Vector2 = Vector2(1, 1)):
self.width = width
self.height = height
self.cells = []
self.cells.resize(height)
for y in range(self.height):
var row : Array = []
row.resize(self.width)
for x in range(self.width):
var cell_data = data[y * self.width + x]
match cell_data:
'#':
row[x] = CellType.Wall
_:
row[x] = CellType.Clear
self.cells[y] = row
self.start_position = start_position

func instance() -> Node:
# Create the scene
var root : Node = Node.new()
root.name = "root"

# Create the ground and add it to the scene
var ground_mesh : PlaneMesh = PlaneMesh.new()
ground_mesh.size = Vector2(self.width * self.CELL_SIZE, self.width * self.CELL_SIZE)
ground_mesh.material = load("res://materials/floor.material")

var ground : MeshInstance = MeshInstance.new()
ground.name = "ground"
ground.mesh = ground_mesh
ground.translation = Vector3(ground_mesh.size.x / 2, 0, ground_mesh.size.y / 2)
root.add_child(ground)

# Create the walls
var mesh_library : MeshLibrary = load("res://meshes/mesh_library.meshlib")
var wall_mesh_id : int = mesh_library.find_item_by_name("wall")

var walls : GridMap = GridMap.new()
walls.mesh_library = mesh_library
for y in range(self.height):
for x in range(self.width):
var cell_type : int = self.cells[y][x]
if cell_type == CellType.Wall:
walls.set_cell_item(x, 0, y, wall_mesh_id)
root.add_child(walls)

return root

func get_start_position() -> Vector3:
return Vector3(
self.start_position.x * self.CELL_SIZE + self.CELL_SIZE / 2,
1,
self.start_position.y * self.CELL_SIZE + self.CELL_SIZE / 2
)

Loading…
Cancel
Save