77 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			77 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| #!/usr/bin/env python
 | |
| from enum import Enum
 | |
| 
 | |
| 
 | |
| 
 | |
| class Map:
 | |
|     width: int
 | |
|     height: int
 | |
|     tiles: list
 | |
| 
 | |
|     def __init__(self, width: int, height: int, tiles: list, entities: list):
 | |
|         self.width = width
 | |
|         self.height = height
 | |
|         self.tiles = tiles
 | |
|         self.entities = entities
 | |
| 
 | |
|     @staticmethod
 | |
|     def load(filename: str):
 | |
|         with open(filename, "r") as f:
 | |
|             file = f.read()
 | |
|         return Map.load_from_string(file)
 | |
| 
 | |
|     @staticmethod
 | |
|     def load_from_string(content: str):
 | |
|         lines = content.split("\n")
 | |
|         lines = [line for line in lines if line]
 | |
|         height = len(lines)
 | |
|         width = len(lines[0])
 | |
|         tiles = [[Tile.from_char(c)
 | |
|                   for x, c in enumerate(line)] for y, line in enumerate(lines)]
 | |
|         return Map(width, height, tiles)
 | |
| 
 | |
|     def draw_string(self) -> str:
 | |
|         return "\n".join("".join(tile.value for tile in line)
 | |
|                          for line in self.tiles)
 | |
| 
 | |
| 
 | |
| class Tile(Enum):
 | |
|     EMPTY = ' '
 | |
|     WALL = '#'
 | |
|     FLOOR = '.'
 | |
| 
 | |
|     @staticmethod
 | |
|     def from_char(c: str):
 | |
|         for t in Tile:
 | |
|             if t.value == c:
 | |
|                 return t
 | |
|         raise ValueError(f"Tile not found : {c}")
 | |
| 
 | |
| 
 | |
| class Entity:
 | |
|     x: int
 | |
|     y: int
 | |
| 
 | |
|     def move(self, x: int, y: int) -> None:
 | |
|         self.x = x
 | |
|         self.y = y
 | |
| 
 | |
| class FightingEntity(Entity):
 | |
|     maxhealth: int
 | |
|     health: int
 | |
|     strength: int
 | |
| 
 | |
|     def __init__(self):
 | |
|         self.health = self.maxhealth
 | |
| 
 | |
|     def hit(self, opponent) -> None:
 | |
|         opponent.take_damage(self, self.strength)
 | |
|     
 | |
|     def take_damage(self, attacker, amount:int) -> None:
 | |
|         self.health -= amount
 | |
|         if self.health <= 0:
 | |
|             self.die()
 | |
|     
 | |
|     def die(self) -> None:
 | |
|        pass 
 |