Improve new entity gestion

This commit is contained in:
Shadow15510 2022-02-27 11:58:37 +01:00
parent 02c7af8007
commit 1984d7ce6c
6 changed files with 42 additions and 52 deletions

26
asci.py
View File

@ -1,17 +1,18 @@
# Asci (1.7.3)
# Asci (1.5.0)
class Asci:
def __init__(self, maps, events_mapping, keys_mapping, behaviors=None, screen_width=21, screen_height=6):
def __init__(self, maps, entities, events_mapping, keys_mapping, behaviors=None, screen_width=21, screen_height=6):
# Load maps and entities
self.maps = []
self.maps = [Map(*i) for i in maps]
self.entities = {}
for index, raw_map in [(i, maps[i]) for i in range(len(maps))]:
for j in raw_map[1]:
if j[0] in self.entities: raise KeyError("'{}' is already a registered entities".format(j[0]))
else: self.entities[j[0]] = Entity(index, *j)
raw_map = list(raw_map)
raw_map.pop(1)
self.maps.append(Map(*raw_map))
entity_id = 0
for i in entities:
if not i[0]:
i[0] = entity_id
entity_id += 1
if i[0] in self.entities: raise KeyError("'{}' is already a registered entities".format(i[0]))
else: self.entities[i[0]] = Entity(*i)
# Custom functions
self._legend = list(events_mapping.keys())
@ -100,6 +101,7 @@ class Asci:
# Update map id and data
old_map, self.data[1] = self.data[1], new_map
self.current_map = self.maps[self.data[1]]
self.current_map.entities = {}
# Update entities
for i in self.entities:
@ -167,7 +169,7 @@ class Asci:
# Configuration
self._legend.append(door)
self._legend.append(walkable)
self._change_map(data[1])
self._change_map(self.data[1])
self.screen.load_data(self.data)
key = 0
@ -287,7 +289,7 @@ class Map:
self.entities = {}
class Entity:
def __init__(self, map_id, entity_id, symbol, x, y, behavior, *args):
def __init__(self, entity_id, symbol, map_id, x, y, behavior, *args):
self.entity_id = entity_id
self.symbol = symbol
self.map_id = map_id

View File

@ -10,11 +10,6 @@ exterieur = (r"""
|==|==|==|==|==|==|==|""",
# Entités
[
("sdf", "*", 2, 5, "stand by")
],
# Points de passage
(1, 3, 1, 3, 5))
@ -27,15 +22,14 @@ interieur = (r"""
| |
+-|^|------+
""",
# Entités
[],
# Points de passage
(3, 5, 0, 1, 3))
carte_monde = (exterieur, interieur)
entites = (
["sdf", "*", 0, 2, 5, "stand by"],
)
def pnj(data, stat, entites, identifiant):
@ -79,5 +73,5 @@ touche = {6: affichage_stat}
def mon_jeu():
rpg_python = Asci(carte_monde, evenements, touche)
rpg_python = Asci(carte_monde, entites, evenements, touche)
rpg_python.mainloop(10, stat=[100, 5], data=[{"main": 0}, 0, 10, 3])

View File

@ -12,9 +12,6 @@ cartes = (
|==|==|==|==|==|==|==|""",
[
(1, "*", 2, 5, "stand by")
],
(1, 3, 1, 5, 7)),
(r"""
@ -26,13 +23,14 @@ cartes = (
+--/ \--------/ \--+
| |
+---|^|--------------+""",
[
("habitant", "?", 9, 1, "stand by"),
("voleur", "?", 20, 1, "stand by")
],
(5, 7, 0, 1, 3))
)
entites = (
[1, "*", 0, 2, 5, "stand by"],
["habitant", "?", 1, 9, 1, "stand by"],
["voleur", "?", 1, 20, 1, "stand by"]
)
def pnj(data, stat, entites, identifiant):
carte_actuelle = data[1]
@ -83,5 +81,5 @@ touche = {8: affichage_statistique}
def mon_jeu():
rpg_python = Asci(cartes, evenements, touche)
rpg_python = Asci(cartes, entites, evenements, touche)
rpg_python.mainloop(11, [100], [{"main": 0}, 0, 10, 3])

View File

@ -15,11 +15,6 @@ cartes = (
#### |<> <>| ####
## |_________| ##
|| ||""",
[
("medecin", "*", 24, 4, "stand by"),
("ami", "*", 16, 1, "stand by"),
("bandit", "$", 4, 7, "walk", 0, ((4, 7), (3, 7), (3, 6), (4, 6)))
],
(1, 3, 1, 5, 7),
(19, 4, 2, 4, 4)),
@ -32,7 +27,6 @@ cartes = (
+--/ \--------/ \--+
| |
+---|^|--------------+""",
[(0, "*", 5, 5, "stand by")],
(5, 7, 0, 1, 3)),
(r"""
@ -42,10 +36,15 @@ cartes = (
| |
+--|^|--+
""",
[],
(4, 4, 0, 19, 4))
)
entites = (
["medecin", "*", 0, 24, 4, "stand by"],
["ami", "*", 0, 16, 1, "stand by"],
["bandit", "$", 0, 4, 7, "walk", 0, ((4, 7), (3, 7), (3, 6), (4, 6))],
[0, "*", 1, 5, 5, "stand by"]
)
def pnj(data, stat, entites, identifiant):
carte_actuelle = data[1]
@ -129,7 +128,7 @@ touche = {7: affichage_stat}
def mon_jeu(stat=[100, 0, 0], data=[{"main": 0}, 0, 10, 3]):
rpg_python = Asci(cartes, evenements, touche)
rpg_python = Asci(cartes, entites, evenements, touche)
stat, data = rpg_python.mainloop(5, stat, data=data)
print("Pour reprendre :")
print("mon_jeu({}, {})".format(stat, data))

View File

@ -12,9 +12,6 @@ cartes = (
|==|==|==|==|==|==|==|""",
[
("pnj", "?", 2, 5, "stand by")
],
(1, 3, 1, 5, 7)),
(r"""
@ -26,13 +23,14 @@ cartes = (
+--/ \--------/ \--+
| |
+---|^|--------------+""",
[
("boulanger", "?", 9, 1, "stand by"),
("kiosque", "?", 20, 1, "stand by")
],
(5, 7, 0, 1, 3))
)
entites = (
["pnj", "?", 0, 2, 5, "stand by"],
["boulanger", "?", 1, 9, 1, "stand by"],
["kiosque", "?", 1, 20, 1, "stand by"]
)
def pnj(data, stat, entites, identifiant):
if identifiant == "pnj":
@ -80,5 +78,5 @@ touche = {8: affichage_statistique}
def mon_jeu():
rpg_python = Asci(cartes, evenements, touche)
stat, data = rpg_python.mainloop(5, [100])
rpg_python = Asci(cartes, entites, evenements, touche)
stat, data = rpg_python.mainloop(5, [100], [{"main": 0}, 0, 10, 3])

View File

@ -19,7 +19,6 @@ def convert_to_string(filename, doors, entities):
doors_coords = []
entities_data = []
entities_count = 0
output = r""
for line_index, line in enumerate(data):
for char_index, char_id in enumerate(line):
@ -28,8 +27,8 @@ def convert_to_string(filename, doors, entities):
output += char_list[char_id]
else:
output += " "
entities_data.append(f"({entities_count}, '{char_list[char_id]}', {char_index}, {line_index}, 'stand_by'),")
entities_count += 1
entities_data.append(f"[None, '{char_list[char_id]}', , {char_index}, {line_index}, 'stand_by'],")
if char_list[char_id] in doors:
doors_coords.append(f"\t({char_index}, {line_index}, , 0, 0),")
@ -37,10 +36,10 @@ def convert_to_string(filename, doors, entities):
output += "\n"
doors_coords = "\n".join(doors_coords)
entities_data = "[\n\t" + "\n\t".join(entities_data) + "\n]"
entities_data = "(\n\t" + "\n\t".join(entities_data) + "\n)"
with open(f"{output_filename}.py", "w") as file:
file.write(f"{output_filename} = (r\"\"\"\n{output[:-1]}\"\"\",\n{entities_data},\n\n{doors_coords}\n)")
file.write(f"{output_filename} = (r\"\"\"\n{output[:-1]}\"\"\",\n{doors_coords}\n)\n\n{output_filename}_entities = {entities_data}")
filename, doors, misc = argv[1], "", ""