2019-11-20 14:19:41 +01:00
|
|
|
tool
|
|
|
|
extends VoxelmanLevelGenerator
|
|
|
|
class_name MainPlanetGenerator
|
|
|
|
|
2020-01-31 20:01:34 +01:00
|
|
|
# Copyright (c) 2019-2020 Péter Magyar
|
2019-12-22 19:20:38 +01:00
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
2019-11-20 14:19:41 +01:00
|
|
|
|
2020-04-26 13:36:10 +02:00
|
|
|
const planet_folder : String = "res://modules/planets"
|
2019-11-20 14:19:41 +01:00
|
|
|
|
|
|
|
export(int) var _force_planet : int = -1
|
|
|
|
export(int) var _level_seed : int
|
|
|
|
export(bool) var _spawn_mobs : bool
|
2020-07-21 23:02:06 +02:00
|
|
|
export(Planet) var planet : Planet = null
|
2019-11-20 14:19:41 +01:00
|
|
|
|
|
|
|
var _world : VoxelWorld
|
|
|
|
var _planet : Planet
|
|
|
|
var _library : VoxelmanLibrary
|
|
|
|
|
|
|
|
func setup(world : VoxelWorld, level_seed : int, spawn_mobs : bool, library: VoxelmanLibrary) -> void:
|
|
|
|
_level_seed = level_seed
|
|
|
|
_world = world
|
|
|
|
_spawn_mobs = spawn_mobs
|
|
|
|
_library = library
|
|
|
|
|
2020-07-18 18:30:39 +02:00
|
|
|
if planet != null:
|
2020-07-21 23:02:06 +02:00
|
|
|
_planet = planet.instance(_level_seed)
|
2020-07-18 12:50:17 +02:00
|
|
|
_planet.current_seed = _level_seed
|
|
|
|
_planet.setup()
|
2020-07-20 21:25:54 +02:00
|
|
|
_planet.setup_library(_library)
|
2020-07-21 00:30:06 +02:00
|
|
|
_library.refresh_rects()
|
2020-07-18 12:50:17 +02:00
|
|
|
|
|
|
|
# create_planet()
|
2019-11-20 14:19:41 +01:00
|
|
|
|
|
|
|
func _generate_chunk(chunk : VoxelChunk) -> void:
|
|
|
|
if _planet == null:
|
|
|
|
return
|
|
|
|
|
|
|
|
_planet.generate_chunk(chunk, _spawn_mobs)
|
|
|
|
|
|
|
|
func create_planet():
|
2020-04-26 10:59:22 +02:00
|
|
|
var planet_files : Array = get_planets(planet_folder)
|
2019-11-20 14:19:41 +01:00
|
|
|
|
|
|
|
if planet_files.size() == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
var ind : int
|
|
|
|
if _force_planet == -1:
|
|
|
|
seed(_level_seed)
|
|
|
|
ind = randi() % planet_files.size()
|
|
|
|
else:
|
|
|
|
ind = _force_planet
|
|
|
|
|
2020-07-21 23:02:06 +02:00
|
|
|
var planet_data : Planet = ResourceLoader.load(planet_files[ind], "Planet")
|
2019-11-20 14:19:41 +01:00
|
|
|
|
|
|
|
if planet_data == null:
|
|
|
|
print("planet_data is null!")
|
|
|
|
return
|
|
|
|
|
|
|
|
print("planet loaded: " + planet_data.resource_path)
|
2020-07-18 12:50:17 +02:00
|
|
|
|
2020-07-21 23:02:06 +02:00
|
|
|
_planet = planet_data.instance(_level_seed)
|
2019-11-20 14:19:41 +01:00
|
|
|
|
|
|
|
_planet.current_seed = _level_seed
|
|
|
|
_planet.data = planet_data
|
|
|
|
_planet.setup()
|
|
|
|
_planet.setup_library(_library)
|
2020-04-26 10:59:22 +02:00
|
|
|
|
|
|
|
func get_planets(path : String, root : bool = true) -> Array:
|
|
|
|
var planet_files : Array = Array()
|
|
|
|
|
|
|
|
var dir = Directory.new()
|
|
|
|
if dir.open(path) == OK:
|
|
|
|
dir.list_dir_begin(true)
|
|
|
|
var file_name = dir.get_next()
|
|
|
|
while (file_name != ""):
|
|
|
|
if not dir.current_is_dir():
|
|
|
|
planet_files.append(path + "/" + file_name)
|
|
|
|
else:
|
|
|
|
if root:
|
|
|
|
var l : Array = get_planets(path + "/" + file_name, false)
|
|
|
|
|
|
|
|
for i in l:
|
|
|
|
planet_files.append(i)
|
|
|
|
|
|
|
|
file_name = dir.get_next()
|
|
|
|
|
|
|
|
return planet_files
|