diff --git a/Global.gd b/Global.gd index 5a0dc52..a39047a 100644 --- a/Global.gd +++ b/Global.gd @@ -3,6 +3,7 @@ extends Node2D var ShowInventory = 0 var LeftClick = 0 var player_inventory_items = [] +var loadedscenes = {} var river_intersection_home_2 = preload("res://river_intersection_home2.tscn").instance() var inventory_screen = preload("res://MiscScenes/Inventory.tscn").instance() var loadgame_screen = null @@ -28,9 +29,20 @@ func AddInventoryItem(itemid, amount): player_inventory_items[x].amount = amount return +func AddScene(scene, savename, loadscene=true): + loadedscenes[savename] = scene + if(loadscene): + GoToScene(savename) + pass + +#Go to schene by name func GoToScene(scene): if current_scene != null: get_tree().get_root().remove_child(current_scene) + if scene in loadedscenes: + current_scene = loadedscenes[scene] + get_tree().get_root().add_child(loadedscenes[scene]) + return match scene: "river_intersection_home_2": current_scene = river_intersection_home_2 diff --git a/MiscCodes/Menu.gd b/MiscCodes/Menu.gd index 14abaec..5a6b191 100644 --- a/MiscCodes/Menu.gd +++ b/MiscCodes/Menu.gd @@ -6,17 +6,20 @@ func _ready(): func _on_Btn_PlayGame_pressed(): # Global.LoadSave() # Global.GoToScene("river_intersection_home_2") + Global.LoadSave() + #create gamemap on the fly var game_data = GlobalStructures.base_tilemap.new() var map_data = [[]] for x in range(150): map_data.append([]) for y in range(150): - map_data[x].append(int(rand_range(0, 77))) + map_data[x].append(int(rand_range(0, 3))) game_data.init_map(150, 150, "res://omgeving/Floor.tres",map_data, "res://omgeving/Floor.tres", map_data, "res://omgeving/Floor.tres", map_data, "res://omgeving/Floor.tres", map_data) var game = load("res://base_tilemap/base_tilemap.tscn").instance() game.load_scene(game_data) - get_tree().get_root().add_child(game) + Global.AddScene(game, "startmap1", true) +# get_tree().get_root().add_child(game) func _on_Btn_LoadGame_pressed(): Global.GoToScene("loadgame_screen") diff --git a/addons/thejustinwalsh.castledb/castledb_importer.gd b/addons/thejustinwalsh.castledb/castledb_importer.gd new file mode 100644 index 0000000..659aa26 --- /dev/null +++ b/addons/thejustinwalsh.castledb/castledb_importer.gd @@ -0,0 +1,96 @@ +tool +extends EditorImportPlugin + +const Utils = preload("res://addons/thejustinwalsh.castledb/castledb_utils.gd") + +enum Presets { PRESET_DEFAULT } + +func get_importer_name(): + return "castledb.importer" + +func get_visible_name(): + return "CastleDB" + +func get_recognized_extensions(): + return ["cdb"] + +func get_save_extension(): + return "gd" + +func get_resource_type(): + return "Script" + +func get_preset_count(): + return Presets.size() + +func get_preset_name(preset): + match preset: + Presets.PRESET_DEFAULT: + return "Default" + _: + return "Unknown" + +func get_import_options(preset): + match preset: + Presets.PRESET_DEFAULT: + return [{ + "name": "default", + "default_value": false + }] + _: + return [] + +func get_option_visibility(option, options): + return true + +func import(source_file, save_path, options, r_platform_variants, r_gen_files): + var file = File.new() + var err = file.open(source_file, File.READ) + if err != OK: + return err + + var json = file.get_as_text() + file.close(); + var json_result = JSON.parse(json) + if json_result.error != OK: + return json_result.error + + var data = json_result.result + var namespace = source_file.get_file().get_basename().capitalize() + + # Script Header + var code = "extends Node" + "\n" + code += "\n" + code += "const CastleDB = preload(\"res://addons/thejustinwalsh.castledb/castledb_types.gd\")" + "\n" + code += "\n" + + # Script child class sheets (namespace) + var sheets:= [] + if data.has("sheets"): + sheets = data["sheets"] + for sheet in sheets: + var name:String = sheet["name"].capitalize().replace(" ", "") + if name.find("@") >= 0: continue # TODO: Implement subclass list data + + var keys = [] + code += "class %s:" % name + "\n" + code += Utils.gen_column_keys(name, sheet["columns"], sheet["lines"], keys, 1) + code += "\n" + code += Utils.gen_column_data(source_file.get_base_dir(), name, sheet["columns"], sheet["lines"], keys, 1) + code += "\n" + + for sheet in sheets: + var name = sheet["name"].capitalize().replace(" ", "") + if name.find("@") >= 0: continue # TODO: Implement subclass list data + + code += "var %s := %s.new()" % [name.to_lower(), name] + "\n" + + var main_script = GDScript.new(); + main_script.set_source_code(code) + + return ResourceSaver.save("%s.%s" % [save_path, get_save_extension()], main_script) + + + + + diff --git a/addons/thejustinwalsh.castledb/castledb_types.gd b/addons/thejustinwalsh.castledb/castledb_types.gd new file mode 100644 index 0000000..fd52b01 --- /dev/null +++ b/addons/thejustinwalsh.castledb/castledb_types.gd @@ -0,0 +1,19 @@ +# Types needed to access CastleDB data +class_name CastleDB + +class Tile: + var file := "" + var size := 0 + var x := 0 + var y := 0 + var stride := 0 + var id := 0 + + func _init(file ="", size = 0, x = 0, y = 0, stride = 0): + self.file = file + self.size = size + self.x = x + self.y = y + self.stride = stride + self.id = y * stride + x + 1 + \ No newline at end of file diff --git a/addons/thejustinwalsh.castledb/castledb_utils.gd b/addons/thejustinwalsh.castledb/castledb_utils.gd new file mode 100644 index 0000000..f964bd5 --- /dev/null +++ b/addons/thejustinwalsh.castledb/castledb_utils.gd @@ -0,0 +1,182 @@ +enum { CDB_ID, CDB_STRING, CDB_BOOL, CDB_INT, CDB_FLOAT, CDB_ENUM, CDB_COLOR, CDB_FILE, CDB_TILE, CDB_NIL } + +static func get_column_type(column): + var type: String = str(column["typeStr"].to_int()) + match type: + "0": + return CDB_ID + "1": + return CDB_STRING + "2": + return CDB_BOOL + "3": + return CDB_INT + "4": + return CDB_FLOAT + "5": + return CDB_ENUM + "11": + return CDB_COLOR + "13": + return CDB_FILE + "14": + return CDB_TILE + _: + return CDB_NIL + +static func gen_castle_types() -> String: + return "" + +static func gen_column_keys(name:String, columns:Array, lines:Array, outKeys:Array, indent:int) -> String: + var tab = "" + for i in indent: + tab += "\t" + + var code = "" + var unique_id = "" + for column in columns: + if get_column_type(column) == CDB_ID: + unique_id = column["name"] + elif get_column_type(column) == CDB_ENUM: + code += tab + "enum %s {" % column["name"].capitalize().strip_edges().replacen(" ", "") + var type = column["typeStr"].split(":", true, 1) + var possible_value = type[type.size() - 1].split(",") + for i in possible_value.size(): + if i > 0: + code += ", " + code += possible_value[i].capitalize().strip_edges().to_upper().replacen(" ", "_") + code += "}" + "\n" + code += "\n" + + if unique_id != "": + for line in lines: + var id = line[unique_id] + outKeys.push_back(id) + code += tab + "const %s := \"%s\"" % [id, id] + "\n" + return code + +static func gen_column_data(path:String, name:String, columns:Array, lines:Array, keys:Array, indent:int) -> String: + var tab = "" + for i in indent: + tab += "\t" + + var code = tab + "class %sRow:" % name + "\n" + var params = [] + var types = [] + for column in columns: + var type = get_column_type(column) + match type: + CDB_ID, CDB_STRING, CDB_FILE: + code += tab + "\t" + "var %s := \"\"" % column["name"] + "\n" + params.push_back(column["name"]) + types.push_back(type) + CDB_BOOL: + code += tab + "\t" + "var %s := false" % column["name"] + "\n" + params.push_back(column["name"]) + types.push_back(type) + CDB_INT, CDB_ENUM: + code += tab + "\t" + "var %s := 0" % column["name"] + "\n" + params.push_back(column["name"]) + types.push_back(type) + CDB_FLOAT: + code += tab + "\t" + "var %s := 0.0" % column["name"] + "\n" + params.push_back(column["name"]) + types.push_back(type) + CDB_COLOR: + code += tab + "\t" + "var %s := Color()" % column["name"] + "\n" + params.push_back(column["name"]) + types.push_back(type) + CDB_TILE: + code += tab + "\t" + "var %s := CastleDB.Tile.new()" % column["name"] + "\n" + params.push_back(column["name"]) + types.push_back(type) + _: + pass + + # Init func + code += tab + "\t\n" + code += tab + "\t" + "func _init(" + for i in params.size(): + var param = params[i] + var type = types[i] + if i > 0: code += ", " + match type: + CDB_ID, CDB_STRING, CDB_FILE: + code += "%s = \"\"" % params[i] + CDB_BOOL: + code += "%s = false" % params[i] + CDB_INT, CDB_ENUM: + code += "%s = 0" % params[i] + CDB_FLOAT: + code += "%s = 0.0" % params[i] + CDB_COLOR: + code += "%s = Color()" % params[i] + CDB_TILE: + code += "%s = CastleDB.Tile.new()" % params[i] + _: + code += "%s = \"\"" % params[i] + code += "):" + "\n" + for param in params: + code += tab +"\t\t" + "self.%s = %s" % [param, param] + "\n" + code += tab + "\n" + + # Data + if lines.size() > 0: + code += tab + "var all = [" + for i in lines.size(): + var line = lines[i]; + code += "%sRow.new(" % name + for j in params.size(): + var param = params[j] + var type = types[j] + if line.has(param): + if j > 0: code += ", " + match type: + CDB_ID: + code += "%s" % line[param] + CDB_BOOL: + code += "%s" % "true" if line[param] else "false" + CDB_INT, CDB_ENUM: + code += "%d" % line[param] + CDB_FLOAT: + code += "%f" % line[param] + CDB_STRING, CDB_FILE: + code += "\"%s\"" % line[param] + CDB_COLOR: + code += "Color(%d)" % line[param] + CDB_TILE: + var img = Image.new() + img.load(path + "/" + line[param]["file"]) + var stride = int(img.get_width() / line[param]["size"]) + code += "CastleDB.Tile.new(\"%s\", %s, %s, %s, %s)" % [ line[param]["file"], line[param]["size"], line[param]["x"], line[param]["y"], stride ] + _: + code += "\"%s\"" % line[param] + code += ")" + if i != lines.size() - 1: + code += ", " + code += "]" + "\n" + + # Index + if keys.size() > 0: + code += tab + "var index = {" + for i in keys.size(): + code += "%s: %s" % [keys[i], i] + if i != keys.size() - 1: + code += ", " + code += "}" + "\n" + code += tab + "\n" + + # Get function + code += tab + "func get(id:String) -> %sRow:" % name + "\n" + code += tab + "\t" + "if index.has(id):" + "\n" + code += tab + "\t\t" + "return all[index[id]]" + "\n" + code += tab + "\t" + "return null" + "\n" + + # Get index function + code += "\n" + code += tab + "func get_index(idx:int) -> %sRow:" % name + "\n" + code += tab + "\t" + "if idx < all.size():" + "\n" + code += tab + "\t\t" + "return all[idx]" + "\n" + code += tab + "\t" + "return null" + "\n" + + return code diff --git a/addons/thejustinwalsh.castledb/plugin.cfg b/addons/thejustinwalsh.castledb/plugin.cfg new file mode 100644 index 0000000..a4337a0 --- /dev/null +++ b/addons/thejustinwalsh.castledb/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="CasteDB" +description="Code generator for CastleDB Database files." +author="Justin Walsh" +version="0.0.1" +script="thejustinwalsh.castledb_importer.gd" \ No newline at end of file diff --git a/addons/thejustinwalsh.castledb/thejustinwalsh.castledb_importer.gd b/addons/thejustinwalsh.castledb/thejustinwalsh.castledb_importer.gd new file mode 100644 index 0000000..68693e5 --- /dev/null +++ b/addons/thejustinwalsh.castledb/thejustinwalsh.castledb_importer.gd @@ -0,0 +1,15 @@ +tool +extends EditorPlugin + +var import_plugin = null + +func get_name(): + return "CastleDB Importer" + +func _enter_tree(): + import_plugin = preload("castledb_importer.gd").new() + add_import_plugin(import_plugin) + +func _exit_tree(): + remove_import_plugin(import_plugin) + import_plugin = null \ No newline at end of file diff --git a/base_tilemap/init.gd b/base_tilemap/init.gd index f97f06e..34abfe4 100644 --- a/base_tilemap/init.gd +++ b/base_tilemap/init.gd @@ -1,18 +1,13 @@ extends Node2D func load_scene(map_data): -# var imageset = load("res://omgeving/Floor.tres") $background.tile_set = load("base_tilemap/tilesets/background_ts.tres") - $background.tile_set.autotile_set_size(1, $background.tile_set.autotile_get_size(0)) - $background.tile_set.autotile_set_size(2, $background.tile_set.autotile_get_size(0)) - $background.tile_set.autotile_set_size(3, $background.tile_set.autotile_get_size(0)) - var k = $background $vegetation.tile_set = load("res://river_intersection_home2.tscn::1") -# $vegetation.tile_set = preload("res://omgeving/Floor.tres") + for x in range(map_data.width): for y in range(map_data.height): $background.set_cell(x, y, map_data.background_map[x][y]) - $vegetation.set_cell(x, y, 0) + $vegetation.set_cell(x, y, -1) $interaction_map.set_cell(x, y, -1) $player_interaction.set_cell(x, y, -1) diff --git a/project.godot b/project.godot index 5ac669e..d03545f 100644 --- a/project.godot +++ b/project.godot @@ -8,9 +8,14 @@ config_version=4 -_global_script_classes=[ ] +_global_script_classes=[ { +"base": "Reference", +"class": "CastleDB", +"language": "GDScript", +"path": "res://addons/thejustinwalsh.castledb/castledb_types.gd" +} ] _global_script_class_icons={ - +"CastleDB": "" } [application] @@ -33,7 +38,7 @@ window/stretch/mode="2d" [editor_plugins] -enabled=PoolStringArray( "vnen.tiled_importer" ) +enabled=PoolStringArray( "thejustinwalsh.castledb", "vnen.tiled_importer" ) [importer_defaults]