Skip to content

Commit

Permalink
Merge branch 'dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
asdawej authored Feb 22, 2024
2 parents 844d728 + 1b79f3f commit 1418434
Show file tree
Hide file tree
Showing 30 changed files with 712 additions and 678 deletions.
31 changes: 31 additions & 0 deletions dependency/MapGenerator-Python/CLR_IMPORT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import CLR_START
import clr
import sys
clr.AddReference('System')
PREPARATION_IMPORT = [r'../../logic/Preparation/bin/Debug/net8.0/',
r'../../logic/Preparation/bin/Release/net8.0/',
r'../../../logic/Preparation/bin/Debug/net8.0/',
r'../../../logic/Preparation/bin/Release/net8.0/',
r'_internal/']
PREPARATION = 'Preparation'
GAMECLASS_IMPORT = [r'../../logic/GameClass/bin/Debug/net8.0/',
r'../../logic/GameClass/bin/Release/net8.0/',
r'../../../logic/GameClass/bin/Debug/net8.0/',
r'../../../logic/GameClass/bin/Release/net8.0/',
r'_internal/']
GAMECLASS = 'GameClass'


def Find(paths: list[str], target: str) -> None:
try:
sys.path.append(paths[0])
clr.AddReference(target)
except BaseException:
paths.pop(0)
if paths == []:
raise FileNotFoundError(f'未找到{target}')
Find(paths, target)


Find(PREPARATION_IMPORT, PREPARATION)
Find(GAMECLASS_IMPORT, GAMECLASS)
3 changes: 3 additions & 0 deletions dependency/MapGenerator-Python/CLR_START.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from clr_loader import get_coreclr
from pythonnet import set_runtime
set_runtime(get_coreclr())
34 changes: 0 additions & 34 deletions dependency/MapGenerator-Python/Classes/AreaRenderDict.py

This file was deleted.

72 changes: 41 additions & 31 deletions dependency/MapGenerator-Python/Classes/MapRenderer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
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
from matplotlib.patches import Rectangle
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


Expand All @@ -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]]
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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:
Expand All @@ -137,19 +149,17 @@ 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

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)
52 changes: 0 additions & 52 deletions dependency/MapGenerator-Python/Classes/MapStruct.py

This file was deleted.

2 changes: 1 addition & 1 deletion dependency/MapGenerator-Python/Classes/RandomCore.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading

0 comments on commit 1418434

Please sign in to comment.