diff --git a/dependency/MapGenerator-Python/Classes/AreaRenderDict.py b/dependency/MapGenerator-Python/Classes/AreaRenderDict.py deleted file mode 100755 index 8672813d..00000000 --- a/dependency/MapGenerator-Python/Classes/AreaRenderDict.py +++ /dev/null @@ -1,34 +0,0 @@ -from __future__ import annotations -from functools import cached_property - - -class AreaRenderDict: - class Unit: - name: str - value: int - color: str - - def __init__(self, _name: str, _value: int, _color: str) -> None: - self.name, self.value, self.color = _name, _value, _color - - areas: list[Unit] - - @cached_property - def Name2Unit(self) -> dict[str, Unit]: - return {x.name: x for x in self.areas} - - @cached_property - def Value2Unit(self) -> dict[int, Unit]: - return {x.value: x for x in self.areas} - - @cached_property - def Color2Unit(self) -> dict[str, Unit]: - return {x.color: x for x in self.areas} - - @cached_property - def Value2Color(self) -> dict[int, str]: - return {x: self.Value2Unit[x].color for x in self.Value2Unit} - - def __init__(self, _areas: list[list[str, int, str]]) -> None: - self.areas = [AreaRenderDict.Unit(_name, _value, _color) - for _name, _value, _color in _areas] diff --git a/dependency/MapGenerator-Python/Classes/MapRenderer.py b/dependency/MapGenerator-Python/Classes/MapRenderer.py index c11b20dc..4ebc558a 100755 --- a/dependency/MapGenerator-Python/Classes/MapRenderer.py +++ b/dependency/MapGenerator-Python/Classes/MapRenderer.py @@ -1,7 +1,8 @@ from __future__ import annotations from queue import Queue +from typing import Any, Generator, NoReturn -from easygui import choicebox +from easygui import choicebox, msgbox import matplotlib.pyplot as plt from matplotlib.figure import Figure from matplotlib.axes import Axes @@ -9,8 +10,9 @@ from matplotlib.ticker import MultipleLocator from matplotlib.backend_bases import MouseEvent, KeyEvent -from Classes.AreaRenderDict import AreaRenderDict -from Classes.MapStruct import MapStruct +from System import String +from GameClass.MapGenerator import MapStruct +from Preparation.Utility import PlaceType as PT from Classes.RandomCore import RandomCore @@ -25,10 +27,30 @@ def __init__(self, _r: int, _c: int, _tp: str) -> None: self.c = _c self.tp = _tp + class CurrentRender: + __cur_gen: Generator[PT, Any, NoReturn] + __cur: PT + + def __init__(self, _areas: dict[PT, str]) -> None: + _keys = list(_areas.keys()) + self.__cur = _keys[0] + + def g(): + while True: + for k in _keys: + yield k + self.__cur_gen = g() + + def Get(self) -> PT: + return self.__cur + + def Switch(self) -> None: + self.__cur = next(self.__cur_gen) + title: str map: MapStruct - areaRender: AreaRenderDict - mapf: str + areas: dict[PT, str] + mapf: String fig: Figure ax: Axes rects: list[list[Rectangle]] @@ -38,17 +60,7 @@ def __init__(self, _r: int, _c: int, _tp: str) -> None: isCursorLift: int # 0: 未提起; 1: 提起未选定; 2: 提起并选定 cursorLift: tuple[int] # 提起坐标 - __curMax: int - __cur: int - - @property - def Cur(self) -> str: - return self.areaRender.areas[self.__cur].color - - @Cur.setter - def Cur(self, _) -> None: - self.__cur += 1 - self.__cur %= self.__curMax + cur: CurrentRender @property def Queue_Render(self) -> RenderUnit: @@ -58,22 +70,21 @@ def Queue_Render(self) -> RenderUnit: def Queue_Render(self, value: RenderUnit) -> None: self.queue_render.put(value, timeout=0.1) - def __init__(self, _title, _mapStruct: MapStruct, _areas: AreaRenderDict, + def __init__(self, _title, _mapStruct: MapStruct, _areas: dict[PT, str], _mapf: str, _randoms: list[RandomCore]) -> None: self.title = _title self.map = _mapStruct - self.areaRender = _areas + self.areas = _areas self.mapf = _mapf self.randomCores = _randoms self.fig, self.ax = plt.subplots() - self.rects = [[Rectangle((j, i), 1, 1, facecolor=self.areaRender.areas[self.map[i, j]].color) + self.rects = [[Rectangle((j, i), 1, 1, facecolor=self.areas[self.map[i, j]]) for j in range(self.map.width)] for i in range(self.map.height)] self.queue_render = Queue() self.isCursorLift = 0 self.cursorLift = None - self.__curMax = len(self.areaRender.areas) - self.__cur = 0 + self.cur = MapRenderer.CurrentRender(_areas) def MainFrame(self) -> None: self.fig.set_size_inches(self.map.width, self.map.height) @@ -99,8 +110,8 @@ def on_click(self, event: MouseEvent) -> None: r, c = int(event.ydata), int(event.xdata) match self.isCursorLift: case 0: - self.map[r, c] = self.areaRender.Color2Unit[self.Cur].value - self.Queue_Render = MapRenderer.RenderUnit(r, c, self.Cur) + self.map[r, c] = self.cur.Get() + self.Queue_Render = MapRenderer.RenderUnit(r, c, self.areas[self.cur.Get()]) self.Render() case 1: self.cursorLift = (r, c) @@ -110,12 +121,12 @@ def on_click(self, event: MouseEvent) -> None: dir_r, dir_c = (1 if liftr <= r else -1), (1 if liftc <= c else -1) for i in range(liftr, r + dir_r, dir_r): for j in range(liftc, c + dir_c, dir_c): - self.map[i, j] = self.areaRender.Color2Unit[self.Cur].value - self.Queue_Render = MapRenderer.RenderUnit(i, j, self.Cur) + self.map[i, j] = self.cur.Get() + self.Queue_Render = MapRenderer.RenderUnit(i, j, self.areas[self.cur.Get()]) self.Render() self.isCursorLift = 0 case 3: - self.Cur = 0 + self.cur.Switch() case _: return @@ -126,7 +137,8 @@ def on_press(self, event: KeyEvent) -> None: case 'z': self.isCursorLift = 1 case 'c': - self.map.ToFile(self.mapf) + MapStruct.ToFile(self.mapf, self.map) + msgbox(msg='Your map has been saved.', title=self.title) case 'p': opt = choicebox(msg='Choose random', title=self.title, choices=[x.Name for x in self.randomCores]) if opt is None: @@ -137,10 +149,8 @@ def on_press(self, event: KeyEvent) -> None: x.Random(self.map) for r in range(self.map.height): for c in range(self.map.width): - self.Queue_Render = MapRenderer.RenderUnit( - r, c, self.areaRender.Value2Color[self.map[r, c]]) + self.Queue_Render = MapRenderer.RenderUnit(r, c, self.areas[self.map[r, c]]) self.Render() - plt.show(block=False) case _: return @@ -148,8 +158,8 @@ def Render(self) -> None: while not self.queue_render.empty(): cur = self.Queue_Render self._render(cur.r, cur.c, cur.tp) + plt.show(block=False) def _render(self, r: int, c: int, tp: str) -> None: self.rects[r][c].set_color(tp) self.ax.draw_artist(self.rects[r][c]) - plt.show(block=False) diff --git a/dependency/MapGenerator-Python/Classes/MapStruct.py b/dependency/MapGenerator-Python/Classes/MapStruct.py deleted file mode 100755 index 3e7c5ec6..00000000 --- a/dependency/MapGenerator-Python/Classes/MapStruct.py +++ /dev/null @@ -1,56 +0,0 @@ -from __future__ import annotations -from functools import cached_property -from io import TextIOWrapper -from typing import Literal, overload -from array import array - - -class MapStruct: - __arrAlloc: array[int] - - @cached_property - def width(self) -> int: - return self.__arrAlloc[1] - - @cached_property - def height(self) -> int: - return self.__arrAlloc[0] - - @property - def ArrayView(self) -> list[list[int]]: - return [[self[i, j] for j in range(self.width)] for i in range(self.height)] - - def __getitem__(self, rowcol: tuple[int, int]) -> int: - return self.__arrAlloc[2 + rowcol[1] + rowcol[0] * self.width] - - def __setitem__(self, rowcol: tuple[int, int], val: int) -> None: - self.__arrAlloc[2 + rowcol[1] + rowcol[0] * self.width] = val - - @overload - def __init__(self, dtype: Literal['b', 'B', 'u', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'f', 'd'], - height: int, width: int) -> None: ... - - @overload - def __init__(self, dtype: Literal['b', 'B', 'u', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'f', 'd'], - mapFile: TextIOWrapper) -> None: ... - - def __init__(self, dtype: Literal['b', 'B', 'u', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'f', 'd'], - *args) -> None: - self.__arrAlloc = array(dtype) - if len(args) == 1: - mapFile = args[0] - self.__arrAlloc.fromfile(mapFile, 2) - self.__arrAlloc.fromfile(mapFile, self.width * self.height) - else: - self.__arrAlloc.append(args[0]) - self.__arrAlloc.append(args[1]) - self.__arrAlloc.fromlist( - [0 for _ in range(self.width * self.height)]) - - def ToFile(self, path: str) -> None: - with open(path, 'wb') as fp: - self.__arrAlloc.tofile(fp) - - def Clear(self) -> None: - for i in range(2, 2 + self.height * self.width): - self.__arrAlloc[i] = 0 diff --git a/dependency/MapGenerator-Python/Classes/RandomCore.py b/dependency/MapGenerator-Python/Classes/RandomCore.py index a783d49e..833140fe 100755 --- a/dependency/MapGenerator-Python/Classes/RandomCore.py +++ b/dependency/MapGenerator-Python/Classes/RandomCore.py @@ -1,7 +1,7 @@ from __future__ import annotations from abc import abstractmethod, abstractproperty -from Classes.MapStruct import MapStruct +from GameClass.MapGenerator import MapStruct class RandomCore: diff --git a/dependency/MapGenerator-Python/Classes/RandomCores/PerlinRandomCore.py b/dependency/MapGenerator-Python/Classes/RandomCores/PerlinRandomCore.py index aa9b3e91..86efc3ed 100755 --- a/dependency/MapGenerator-Python/Classes/RandomCores/PerlinRandomCore.py +++ b/dependency/MapGenerator-Python/Classes/RandomCores/PerlinRandomCore.py @@ -4,15 +4,10 @@ from easygui import multenterbox from perlin_noise import PerlinNoise -from Classes.MapStruct import MapStruct +from GameClass.MapGenerator import MapStruct +from Preparation.Utility import PlaceType as PT from Classes.RandomCore import RandomCore -dict_place2num = { - 'Space': 0, - 'Ruin': 1, - 'Shadow': 2 -} - class PerlinRandomCore(RandomCore): title: str @@ -87,15 +82,15 @@ def Random(self, mp: MapStruct) -> None: for i in range(mp.height): for j in range(mp.width): if i == 0 or i == mp.height - 1: - mp[i, j] = dict_place2num['Ruin'] + mp[i, j] = PT.Ruin continue elif j == 0 or j == mp.width - 1: - mp[i, j] = dict_place2num['Ruin'] + mp[i, j] = PT.Ruin continue cur = arr[i][j] if arr0 < cur < arr1: - mp[i, j] = dict_place2num['Shadow'] + mp[i, j] = PT.Shadow elif arr1 < cur < arr2: - mp[i, j] = dict_place2num['Space'] + mp[i, j] = PT.Null else: - mp[i, j] = dict_place2num['Ruin'] + mp[i, j] = PT.Ruin diff --git "a/dependency/MapGenerator-Python/Classes/RandomCores/\320\241\321\216\320\271\320\247\321\215\320\275RandomCore.py" "b/dependency/MapGenerator-Python/Classes/RandomCores/\320\241\321\216\320\271\320\247\321\215\320\275RandomCore.py" index d8f61a5a..09401dd8 100755 --- "a/dependency/MapGenerator-Python/Classes/RandomCores/\320\241\321\216\320\271\320\247\321\215\320\275RandomCore.py" +++ "b/dependency/MapGenerator-Python/Classes/RandomCores/\320\241\321\216\320\271\320\247\321\215\320\275RandomCore.py" @@ -4,7 +4,8 @@ from easygui import multenterbox -from Classes.MapStruct import MapStruct +from GameClass.MapGenerator import MapStruct +from Preparation.Utility import PlaceType as PT from Classes.RandomCore import RandomCore @@ -175,12 +176,12 @@ def Random(self, mp: MapStruct) -> None: def isEmptyNearby(mp: MapStruct, x: int, y: int, r: int) -> bool: for i in range(x - r if x - r >= 0 else 0, (x + r if x + r <= 49 else 49) + 1): for j in range(y - r if y - r >= 0 else 0, (y - r if y + r <= 9 else 49) + 1): - if mp[i, j] != 0: + if mp[i, j] != PT.Null: return False return True @staticmethod - def haveSthNearby(mp: MapStruct, x: int, y: int, r: int, tp: int) -> int: + def haveSthNearby(mp: MapStruct, x: int, y: int, r: int, tp: PT) -> int: ret = 0 for i in range(x - r if x - r >= 0 else 0, (x + r if x + r <= 49 else 49) + 1): for j in range(y - r if y - r >= 0 else 0, (y - r if y + r <= 9 else 49) + 1): @@ -189,7 +190,7 @@ def haveSthNearby(mp: MapStruct, x: int, y: int, r: int, tp: int) -> int: return ret @staticmethod - def haveSthCross(mp: MapStruct, x: int, y: int, r: int, tp: int) -> int: + def haveSthCross(mp: MapStruct, x: int, y: int, r: int, tp: PT) -> int: ret = 0 for i in range(x - r if x - r >= 0 else 0, (x + r if x + r <= 49 else 49) + 1): if mp[i, y] == tp: @@ -202,28 +203,28 @@ def haveSthCross(mp: MapStruct, x: int, y: int, r: int, tp: int) -> int: @staticmethod def generateBorderRuin(mp: MapStruct) -> None: for i in range(50): - mp[i, 0] = 1 - mp[i, 49] = 1 - mp[0, i] = 1 - mp[49, i] = 1 + mp[i, 0] = PT.Ruin + mp[i, 49] = PT.Ruin + mp[0, i] = PT.Ruin + mp[49, i] = PT.Ruin @staticmethod def generateHome(mp: MapStruct) -> None: - mp[3, 46] = 7 - mp[46, 3] = 7 + mp[3, 46] = PT.Home + mp[46, 3] = PT.Home @staticmethod def generateAsteroid(mp: MapStruct, width: int = DefaultСюйЧэнRandomSettings.asteroidWidth) -> None: for i in range(1, 49): for j in range(24, 24 - width, -1): - mp[i, j] = 3 - mp[49 - i, 49 - j] = 3 + mp[i, j] = PT.Asteroid + mp[49 - i, 49 - j] = PT.Asteroid for i in range(1, 23): if random() < 0.5 and i != 9 and i != 10 and i != 11 and i != 12: - mp[i, 24 - width] = 3 - mp[i, 24 + width] = 0 - mp[49 - i, 25 + width] = 3 - mp[49 - i, 25 - width] = 0 + mp[i, 24 - width] = PT.Asteroid + mp[i, 24 + width] = PT.Null + mp[49 - i, 25 + width] = PT.Asteroid + mp[49 - i, 25 - width] = PT.Null @staticmethod def generateResource(mp: MapStruct, num: int = DefaultСюйЧэнRandomSettings.resourceNum) -> None: @@ -232,8 +233,8 @@ def generateResource(mp: MapStruct, num: int = DefaultСюйЧэнRandomSettings x = floor(random() * 48) + 1 y = floor(random() * 23) + 1 if СюйЧэнRandomCore.isEmptyNearby(mp, x, y, 2): - mp[x, y] = 4 - mp[49 - x, 49 - y] = 4 + mp[x, y] = PT.Resource + mp[49 - x, 49 - y] = PT.Resource else: i -= 1 i += 1 @@ -245,8 +246,8 @@ def generateConstruction(mp: MapStruct, num: int = DefaultСюйЧэнRandomSett x = floor(random() * 48) + 1 y = floor(random() * 23) + 1 if СюйЧэнRandomCore.isEmptyNearby(mp, x, y, 1): - mp[x, y] = 5 - mp[49 - x, 49 - y] = 5 + mp[x, y] = PT.Construction + mp[49 - x, 49 - y] = PT.Construction else: i -= 1 i += 1 @@ -256,35 +257,35 @@ def generateShadow(mp: MapStruct, prob: float = DefaultСюйЧэнRandomSetting crossBonus: int = DefaultСюйЧэнRandomSettings.shadowCrossBonus) -> None: for i in range(50): for j in range(50): - if (mp[i, j] == 0 and - random() < prob * (СюйЧэнRandomCore.haveSthCross(mp, i, j, 1, 2) * crossBonus + 1)): - mp[i, j] = 2 - mp[49 - i, 49 - j] = 2 + if (mp[i, j] == PT.Null and + random() < prob * (СюйЧэнRandomCore.haveSthCross(mp, i, j, 1, PT.Shadow) * crossBonus + 1)): + mp[i, j] = PT.Shadow + mp[49 - i, 49 - j] = PT.Shadow @staticmethod def generateRuin(mp: MapStruct, prob: float = DefaultСюйЧэнRandomSettings.ruinProb, crossBonus: int = DefaultСюйЧэнRandomSettings.ruinCrossBonus) -> None: for i in range(2, 48): for j in range(2, 48): - if ((mp[i, j] == 0 or mp[i, j] == 2) and - not СюйЧэнRandomCore.haveSthNearby(mp, i, j, 1, 3) and - not СюйЧэнRandomCore.haveSthNearby(mp, i, j, 1, 7) and + if ((mp[i, j] == PT.Null or mp[i, j] == PT.Shadow) and + not СюйЧэнRandomCore.haveSthNearby(mp, i, j, 1, PT.Asteroid) and + not СюйЧэнRandomCore.haveSthNearby(mp, i, j, 1, PT.Home) and random() < prob - * (СюйЧэнRandomCore.haveSthCross(mp, i, j, 1, 1) - * (0 if СюйЧэнRandomCore.haveSthCross(mp, i, j, 1, 1) > 1 + * (СюйЧэнRandomCore.haveSthCross(mp, i, j, 1, PT.Ruin) + * (0 if СюйЧэнRandomCore.haveSthCross(mp, i, j, 1, PT.Ruin) > 1 else crossBonus) + 1)): - mp[i, j] = 1 - mp[49 - i, 49 - j] = 1 + mp[i, j] = PT.Ruin + mp[49 - i, 49 - j] = PT.Ruin @staticmethod def generateWormhole(mp: MapStruct) -> None: for i in range(1, 49): - if mp[10, i] == 3: - mp[10, i] = 6 - mp[39, 49 - i] = 6 - if mp[11, i] == 3: - mp[11, i] = 6 - mp[38, 49 - i] = 6 - if mp[24, i] == 3: - mp[24, i] = 6 - mp[25, 49 - i] = 6 + if mp[10, i] == PT.Asteroid: + mp[10, i] = PT.Wormhole + mp[39, 49 - i] = PT.Wormhole + if mp[11, i] == PT.Asteroid: + mp[11, i] = PT.Wormhole + mp[38, 49 - i] = PT.Wormhole + if mp[24, i] == PT.Asteroid: + mp[24, i] = PT.Wormhole + mp[25, 49 - i] = PT.Wormhole diff --git a/dependency/MapGenerator-Python/GameClass/MapGenerator.pyi b/dependency/MapGenerator-Python/GameClass/MapGenerator.pyi index ea4b4d19..0b28b7a7 100644 --- a/dependency/MapGenerator-Python/GameClass/MapGenerator.pyi +++ b/dependency/MapGenerator-Python/GameClass/MapGenerator.pyi @@ -1,3 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + # Stub for C# from __future__ import annotations @@ -13,16 +16,22 @@ class MapStruct: map: Array[PT] @overload - def MapStruct(self, height: UInt32, width: UInt32) -> None: ... + def __init__(self, height: UInt32, width: UInt32) -> None: ... @overload - def MapStruct(self, height: UInt32, width: UInt32, map: Array[PT]) -> None: ... + def __init__(self, height: UInt32, width: UInt32, map: Array[PT]) -> None: ... @overload - def MapStruct(self, height: UInt32, width: UInt32, map: Array[UInt32]) -> None: ... + def __init__(self, height: UInt32, width: UInt32, map: Array[UInt32]) -> None: ... @staticmethod def FromFile(filename: String) -> MapStruct: ... @staticmethod def ToFile(filename: String, src: MapStruct) -> None: ... + + def Clear(self) -> None: ... + + def __getitem__(self, i: UInt32, j: UInt32) -> PT: ... + + def __setitem__(self, i: UInt32, j: UInt32, value: PT) -> None: ... diff --git a/dependency/MapGenerator-Python/MapGenerator.spec b/dependency/MapGenerator-Python/MapGenerator.spec index 124c419b..333e8673 100755 --- a/dependency/MapGenerator-Python/MapGenerator.spec +++ b/dependency/MapGenerator-Python/MapGenerator.spec @@ -5,7 +5,9 @@ a = Analysis( ['main.py'], pathex=[], binaries=[], - datas=[('Settings.json', '.')], + datas=[('../../logic/Preparation/bin/Debug/net8.0/Preparation.dll', '.'), + ('../../logic/GameClass/bin/Debug/net8.0/GameClass.dll', '.') + ], hiddenimports=[], hookspath=[], hooksconfig={}, diff --git a/dependency/MapGenerator-Python/Preparation/Utility.pyi b/dependency/MapGenerator-Python/Preparation/Utility.pyi index 9c27d3e5..413ae584 100644 --- a/dependency/MapGenerator-Python/Preparation/Utility.pyi +++ b/dependency/MapGenerator-Python/Preparation/Utility.pyi @@ -1,3 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + # Stub for C# from __future__ import annotations diff --git a/dependency/MapGenerator-Python/SETTINGS.py b/dependency/MapGenerator-Python/SETTINGS.py new file mode 100644 index 00000000..b1c8af4e --- /dev/null +++ b/dependency/MapGenerator-Python/SETTINGS.py @@ -0,0 +1,12 @@ +from Preparation.Utility import PlaceType as PT + +title = 'THUAI7-MapGenerator' +file_suffix = '.thuai7.map' +areas = {PT.Null: '#FFFFFF', + PT.Ruin: '#B97A57', + PT.Shadow: '#22B14C', + PT.Asteroid: '#99D9EA', + PT.Resource: '#A349A4', + PT.Construction: '#FF7F27', + PT.Wormhole: '#880015', + PT.Home: '#ED1C24'} diff --git a/dependency/MapGenerator-Python/Settings.json b/dependency/MapGenerator-Python/Settings.json deleted file mode 100755 index db949be3..00000000 --- a/dependency/MapGenerator-Python/Settings.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "title": "THUAI7-MapGenerator", - "file_suffix": ".thuai7.map", - "dtype": "B", - "areas": [ - [ - "Space", - 0, - "#FFFFFF" - ], - [ - "Ruin", - 1, - "#B97A57" - ], - [ - "Shadow", - 2, - "#22B14C" - ], - [ - "Asteroid", - 3, - "#99D9EA" - ], - [ - "Resource", - 4, - "#A349A4" - ], - [ - "Construction", - 5, - "#FF7F27" - ], - [ - "Wormhole", - 6, - "#880015" - ], - [ - "Home", - 7, - "#ED1C24" - ] - ] -} \ No newline at end of file diff --git a/dependency/MapGenerator-Python/System.pyi b/dependency/MapGenerator-Python/System.pyi index 6db3600d..2e34aeda 100644 --- a/dependency/MapGenerator-Python/System.pyi +++ b/dependency/MapGenerator-Python/System.pyi @@ -1,14 +1,17 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + # Stub for C# from __future__ import annotations class UInt32: - def String(self, i: int) -> None: ... + def __init__(self, i: int) -> None: ... class String: - def String(self, s: str) -> None: ... + def __init__(self, s: str) -> None: ... class Array[T]: diff --git a/dependency/MapGenerator-Python/main.py b/dependency/MapGenerator-Python/main.py index 2c48ab1e..94842829 100755 --- a/dependency/MapGenerator-Python/main.py +++ b/dependency/MapGenerator-Python/main.py @@ -1,50 +1,32 @@ from __future__ import annotations from io import TextIOWrapper -import json import os import os.path from easygui import multenterbox -from Classes.AreaRenderDict import AreaRenderDict -from Classes.MapStruct import MapStruct +import CLR_IMPORT +import SETTINGS +from System import UInt32, String +from GameClass.MapGenerator import MapStruct from Classes.MapRenderer import MapRenderer from Classes.RandomCores.PerlinRandomCore import PerlinRandomCore from Classes.RandomCores.СюйЧэнRandomCore import СюйЧэнRandomCore - -# 查找设置 -SETTINGS_PATH = '' -TARGET_SETTINGS_PATH = 'Settings.json' -for root, _, files in os.walk('.'): - if TARGET_SETTINGS_PATH in files: - SETTINGS_PATH = os.path.join(root, TARGET_SETTINGS_PATH) -if SETTINGS_PATH == '': - raise FileNotFoundError('未找到设置文件') -# 加载设置 -with open(SETTINGS_PATH, 'r', encoding='utf-8') as jsonfp: - SETTINGS = json.load(jsonfp) - TITLE: str = SETTINGS['title'] - FILE_SUFFIX: str = SETTINGS['file_suffix'] - DTYPE: str = SETTINGS['dtype'] - AREAS: AreaRenderDict = AreaRenderDict(SETTINGS['areas']) # 获取路径 -path: str = multenterbox(msg='', title=TITLE, fields=[f'Path(*{FILE_SUFFIX})'])[0] -if path[-len(FILE_SUFFIX):] != FILE_SUFFIX: - path += FILE_SUFFIX +path: str = multenterbox(msg='', title=SETTINGS.title, fields=[f'Path(*{SETTINGS.file_suffix})'])[0] +if path[-len(SETTINGS.file_suffix):] != SETTINGS.file_suffix: + path += SETTINGS.file_suffix # 地图加载 -mapfile: TextIOWrapper -mapStruct: MapStruct if not os.path.exists(path): - height, width = [int(x) for x in multenterbox(msg='Create new map', title=TITLE, fields=['Height', 'Width'])] - mapStruct = MapStruct(DTYPE, height, width) - mapStruct.ToFile(path) + height, width = [UInt32(int(x)) + for x in multenterbox(msg='Create new map', title=SETTINGS.title, fields=['Height', 'Width'])] + mapStruct = MapStruct(height, width) + MapStruct.ToFile(String(path), mapStruct) else: - mapfile = open(path, 'r+b') - mapStruct = MapStruct(DTYPE, mapfile) - mapfile.close() + mapStruct = MapStruct.FromFile(String(path)) # 随机核加载 -randomCores = [СюйЧэнRandomCore(TITLE), PerlinRandomCore(TITLE)] +randomCores = [СюйЧэнRandomCore(SETTINGS.title), PerlinRandomCore(SETTINGS.title)] # 地图渲染 -mapRenderer = MapRenderer(TITLE, mapStruct, AREAS, path, randomCores) +mapRenderer = MapRenderer(SETTINGS.title, mapStruct, SETTINGS.areas, String(path), randomCores) mapRenderer.MainFrame() diff --git a/logic/GameClass/GameObj/Map/MapStruct.cs b/logic/GameClass/GameObj/Map/MapStruct.cs index 20e15947..39c96325 100644 --- a/logic/GameClass/GameObj/Map/MapStruct.cs +++ b/logic/GameClass/GameObj/Map/MapStruct.cs @@ -71,4 +71,16 @@ public static void ToFile(string filename, MapStruct src) } } } + public void Clear() + { + for (uint i = 0; i < height; i++) + { + for (uint j = 0; j < width; j++) map[i, j] = PlaceType.Null; + } + } + public PlaceType this[uint i, uint j] + { + get => map[i, j]; + set => map[i, j] = value; + } } \ No newline at end of file