-
Notifications
You must be signed in to change notification settings - Fork 0
LNX-64 Bmp map parsing #11
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pytest==7.2.0 | ||
transitions==0.9.0 | ||
transitions==0.9.0 | ||
pillow==9.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
"floor_colour": "(34, 177, 76)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary for |
||
"functions" : [ | ||
{"(0, 0, 0)" : "TrainingDummy"}, | ||
{"(237, 28, 36)" : "Portal"}, | ||
{"(255, 201, 14)" : "self._init_agent"} ] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import random | ||
from typing import Dict, List | ||
|
||
from src.common.point import Point | ||
from src.objects.agent import Agent | ||
from src.objects.floor import Floor | ||
from src.objects.portal import Portal | ||
from src.objects.object import Object | ||
from src.objects.npcs.enemies.training_dummy import TrainingDummy | ||
|
||
|
||
import json | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Group exports in accordance to PEP 8 Python standard.
Please use more blank lines, also. |
||
from PIL import Image | ||
PATH_TO_CONFIG = "./src/init_files/config.json" | ||
class Scene: | ||
""" | ||
Object representing one level in the game. | ||
|
@@ -27,20 +27,16 @@ def __init__(self, runtime) -> None: | |
self._objects_id_map = {} | ||
self.agent_locals = {} | ||
self.runtime = runtime | ||
|
||
self._generate_scene_small() | ||
|
||
self.__player = Agent(self) | ||
self._generate_scene() | ||
self.__player = None | ||
|
||
def _generate_scene_small(self): | ||
""" | ||
Small scene for tests | ||
""" | ||
points = [Point(0, 0), Point(0, 1), Point(1, 0), Point(1, 1), Point(0, 2), Point(1, 2)] | ||
|
||
for point in points: | ||
Floor(self, point) | ||
|
||
#Portal(self, random.choice(points)) | ||
#TrainingDummy(self, Point(1, 0)) | ||
|
||
|
@@ -49,24 +45,23 @@ def _generate_scene(self): | |
For now it'll generate scene layout | ||
Subject to be changed | ||
""" | ||
STEPS = 20 | ||
pos = Point(0, 0) | ||
points = [pos] | ||
for _ in range(STEPS): | ||
pos += Point(random.choice([-1, 0, 1]), random.choice([-1, 0, 1])) | ||
points.append(pos) | ||
points.append(pos + Point(-1, 0)) | ||
points.append(pos + Point(1, 0)) | ||
points.append(pos + Point(0, -1)) | ||
points.append(pos + Point(0, 1)) | ||
|
||
points = list(dict.fromkeys(points)) # remove duplicates | ||
|
||
for point in points: | ||
Floor(self, point) | ||
|
||
Portal(self, random.choice(points)) | ||
TrainingDummy(self, Point(1, 0)) | ||
with open(PATH_TO_CONFIG) as json_file: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we move it to separate file and class? BMPMap.load(self, "./src/maps/map1.bmp") or something like that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes please! |
||
config = json.load(json_file) | ||
floor_colour = config["floor_colour"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I appreciate the British English spelling of word There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Superior American writing requires less less typing with the same clarity of the concept's meaning 🥰 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gigachad |
||
colour_to_function_dict = {} | ||
for dict_row in config["functions"]: | ||
colour_to_function_dict.update(dict_row) | ||
map = Image.open("./src/maps/map1.bmp") | ||
image_width, image_height = map.size | ||
for width in range(image_width): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Divide into helper functions, 57-63 especially. |
||
for height in range(image_height): | ||
pixel_colour = str(map.getpixel((width, height))) | ||
if pixel_colour in colour_to_function_dict or pixel_colour == floor_colour: | ||
Floor(self, Point(width, height)) | ||
if pixel_colour != floor_colour: | ||
eval(colour_to_function_dict[pixel_colour])(self, Point(width, height)) | ||
map.close() | ||
|
||
def run(self): | ||
""" | ||
|
@@ -144,3 +139,6 @@ def add_object_to_id_map(self, new_object: Object) -> None: | |
Add object to per scene storage indexed by objects `id`. | ||
""" | ||
self._objects_id_map[new_object.properties.id] = new_object | ||
|
||
def _init_agent(self, _, points): | ||
self.__player = Agent(self, points) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've switched to
StateMachine
of @alpinus4, remove that line.