47
									
								
								dungeonbattle/interfaces.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								dungeonbattle/interfaces.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					#!/usr/bin/env python
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Map:
 | 
				
			||||||
 | 
					    width: int
 | 
				
			||||||
 | 
					    height: int
 | 
				
			||||||
 | 
					    tiles: list
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __init__(self, width: int, height: int, tiles: list):
 | 
				
			||||||
 | 
					        self.width = width
 | 
				
			||||||
 | 
					        self.height = height
 | 
				
			||||||
 | 
					        self.tiles = tiles
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def load(filename):
 | 
				
			||||||
 | 
					        with open(filename, "r") as f:
 | 
				
			||||||
 | 
					            file = f.read()
 | 
				
			||||||
 | 
					        return Map.load_from_string(file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def load_from_string(content):
 | 
				
			||||||
 | 
					        lines = content.split("\n")
 | 
				
			||||||
 | 
					        height = len(lines)
 | 
				
			||||||
 | 
					        width = len(lines[0]) - 1
 | 
				
			||||||
 | 
					        chars = [[Tile.from_char(c, x, y) for x, c in enumerate(line)] for y, line in enumerate(lines)]
 | 
				
			||||||
 | 
					        return Map(width, height, chars)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Tile:
 | 
				
			||||||
 | 
					    x: int
 | 
				
			||||||
 | 
					    y: int
 | 
				
			||||||
 | 
					    char: str
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def from_char(c: str, x: int, y: int):
 | 
				
			||||||
 | 
					        t = Tile()
 | 
				
			||||||
 | 
					        t.x = x
 | 
				
			||||||
 | 
					        t.y = y
 | 
				
			||||||
 | 
					        t.char = c
 | 
				
			||||||
 | 
					        return c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Entity:
 | 
				
			||||||
 | 
					    tile: Tile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def move(self, x, y):
 | 
				
			||||||
 | 
					        self.tile.x = x
 | 
				
			||||||
 | 
					        self.tile.y = y
 | 
				
			||||||
							
								
								
									
										10
									
								
								dungeonbattle/interfaces_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								dungeonbattle/interfaces_test.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					import unittest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from dungeonbattle.interfaces import Map
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestInterfaces(unittest.TestCase):
 | 
				
			||||||
 | 
					    def test_map(self):
 | 
				
			||||||
 | 
					        m = Map.load_from_string("ab\ncd\n")
 | 
				
			||||||
 | 
					        self.assertEqual(m.width, 2)
 | 
				
			||||||
 | 
					        self.assertEqual(m.height, 2)
 | 
				
			||||||
		Reference in New Issue
	
	Block a user