Merge branch 'master' into 'game'
# Conflicts: # dungeonbattle/game.py # dungeonbattle/mapdisplay.py
This commit is contained in:
		@@ -4,3 +4,15 @@ from ..interfaces import FightingEntity
 | 
			
		||||
class Player(FightingEntity):
 | 
			
		||||
    maxhealth = 20
 | 
			
		||||
    strength = 5
 | 
			
		||||
 | 
			
		||||
    def move_up(self) -> bool:
 | 
			
		||||
        return self.check_move(self.y - 1, self.x, True)
 | 
			
		||||
 | 
			
		||||
    def move_down(self) -> bool:
 | 
			
		||||
        return self.check_move(self.y + 1, self.x, True)
 | 
			
		||||
 | 
			
		||||
    def move_left(self) -> bool:
 | 
			
		||||
        return self.check_move(self.y, self.x - 1, True)
 | 
			
		||||
 | 
			
		||||
    def move_right(self) -> bool:
 | 
			
		||||
        return self.check_move(self.y, self.x + 1, True)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
import sys
 | 
			
		||||
from typing import Any
 | 
			
		||||
 | 
			
		||||
from .entities.player import Player
 | 
			
		||||
from .interfaces import Map
 | 
			
		||||
from .mapdisplay import MapDisplay
 | 
			
		||||
from .settings import Settings
 | 
			
		||||
@@ -38,8 +39,8 @@ class Game:
 | 
			
		||||
        # TODO generate a new map procedurally
 | 
			
		||||
        self.m = Map.load("example_map.txt")
 | 
			
		||||
        self.player = Player()
 | 
			
		||||
        self.player.y = 1
 | 
			
		||||
        self.player.x = 6
 | 
			
		||||
        self.player.move(y, x)
 | 
			
		||||
        self.m.add_entity(self.player)
 | 
			
		||||
        self.d = MapDisplay(self.m, self.player)
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
@@ -94,21 +95,3 @@ class Game:
 | 
			
		||||
                    self.state = GameMode.SETTINGS
 | 
			
		||||
                elif option == menus.MainMenuValues.EXIT:
 | 
			
		||||
                    sys.exit(0)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Player:
 | 
			
		||||
    # FIXME Should be elsewhere, only useful to don't break the previous code
 | 
			
		||||
    y: int = 0
 | 
			
		||||
    x: int = 0
 | 
			
		||||
 | 
			
		||||
    def move_up(self) -> None:
 | 
			
		||||
        self.y -= 1
 | 
			
		||||
 | 
			
		||||
    def move_down(self) -> None:
 | 
			
		||||
        self.y += 1
 | 
			
		||||
 | 
			
		||||
    def move_left(self) -> None:
 | 
			
		||||
        self.x -= 1
 | 
			
		||||
 | 
			
		||||
    def move_right(self) -> None:
 | 
			
		||||
        self.x += 1
 | 
			
		||||
 
 | 
			
		||||
@@ -11,12 +11,18 @@ class Map:
 | 
			
		||||
    height: int
 | 
			
		||||
    tiles: list
 | 
			
		||||
 | 
			
		||||
    def __init__(self, width: int, height: int, tiles: list,
 | 
			
		||||
                 entities: list = None):
 | 
			
		||||
    def __init__(self, width: int, height: int, tiles: list):
 | 
			
		||||
        self.width = width
 | 
			
		||||
        self.height = height
 | 
			
		||||
        self.tiles = tiles
 | 
			
		||||
        self.entities = entities or []
 | 
			
		||||
        self.entities = []
 | 
			
		||||
 | 
			
		||||
    def add_entity(self, entity: "Entity") -> None:
 | 
			
		||||
        """
 | 
			
		||||
        Register a new entity in the map.
 | 
			
		||||
        """
 | 
			
		||||
        self.entities.append(entity)
 | 
			
		||||
        entity.map = self
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def load(filename: str):
 | 
			
		||||
@@ -39,7 +45,7 @@ class Map:
 | 
			
		||||
        tiles = [[Tile(c)
 | 
			
		||||
                  for x, c in enumerate(line)] for y, line in enumerate(lines)]
 | 
			
		||||
 | 
			
		||||
        return Map(width, height, tiles, [])
 | 
			
		||||
        return Map(width, height, tiles)
 | 
			
		||||
 | 
			
		||||
    def draw_string(self) -> str:
 | 
			
		||||
        """
 | 
			
		||||
@@ -68,11 +74,19 @@ class Tile(Enum):
 | 
			
		||||
class Entity:
 | 
			
		||||
    y: int
 | 
			
		||||
    x: int
 | 
			
		||||
    map: Map
 | 
			
		||||
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        self.y = 0
 | 
			
		||||
        self.x = 0
 | 
			
		||||
 | 
			
		||||
    def check_move(self, y: int, x: int, move_if_possible: bool = False)\
 | 
			
		||||
            -> bool:
 | 
			
		||||
        tile = self.map.tiles[y][x]
 | 
			
		||||
        if tile.can_walk() and move_if_possible:
 | 
			
		||||
            self.move(y, x)
 | 
			
		||||
        return tile.can_walk()
 | 
			
		||||
 | 
			
		||||
    def move(self, y: int, x: int) -> None:
 | 
			
		||||
        self.y = y
 | 
			
		||||
        self.x = x
 | 
			
		||||
 
 | 
			
		||||
@@ -1,22 +1,21 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
import curses
 | 
			
		||||
from typing import Any
 | 
			
		||||
 | 
			
		||||
from dungeonbattle.entities.player import Player
 | 
			
		||||
from dungeonbattle.interfaces import Map
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MapDisplay:
 | 
			
		||||
    def __init__(self, m: Map, player: Any):
 | 
			
		||||
        # TODO Type the player field with the good type
 | 
			
		||||
    def __init__(self, m: Map, player: Player):
 | 
			
		||||
        self.map = m
 | 
			
		||||
        self.pad = curses.newpad(m.height, m.width + 1)
 | 
			
		||||
        self.player = player
 | 
			
		||||
 | 
			
		||||
    def update_pad(self) -> None:
 | 
			
		||||
        self.pad.addstr(0, 0, self.map.draw_string())
 | 
			
		||||
        # TODO Not all entities should be a player
 | 
			
		||||
        for e in self.map.entities:
 | 
			
		||||
            self.pad.addch(e.y, e.x, e.img)
 | 
			
		||||
        self.pad.addstr(self.player.y, self.player.x, '🐿️')
 | 
			
		||||
            self.pad.addstr(e.y, e.x, '🐿')
 | 
			
		||||
 | 
			
		||||
    def display(self, y: int, x: int) -> None:
 | 
			
		||||
        deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS // 2) + 1
 | 
			
		||||
 
 | 
			
		||||
@@ -59,8 +59,8 @@ class TestEntities(unittest.TestCase):
 | 
			
		||||
        """
 | 
			
		||||
        item = Bomb()
 | 
			
		||||
        squirrel = Squirrel()
 | 
			
		||||
        self.map.entities.append(item)
 | 
			
		||||
        self.map.entities.append(squirrel)
 | 
			
		||||
        self.map.add_entity(item)
 | 
			
		||||
        self.map.add_entity(squirrel)
 | 
			
		||||
        squirrel.health = 2
 | 
			
		||||
        squirrel.move(41, 42)
 | 
			
		||||
        item.act(self.map)
 | 
			
		||||
@@ -76,6 +76,22 @@ class TestEntities(unittest.TestCase):
 | 
			
		||||
        Test some random stuff with players.
 | 
			
		||||
        """
 | 
			
		||||
        player = Player()
 | 
			
		||||
        self.map.add_entity(player)
 | 
			
		||||
        player.move(1, 6)
 | 
			
		||||
        self.assertEqual(player.strength, 5)
 | 
			
		||||
        self.assertEqual(player.health, player.maxhealth)
 | 
			
		||||
        self.assertEqual(player.maxhealth, 20)
 | 
			
		||||
 | 
			
		||||
        # Test movements and ensure that collisions are working
 | 
			
		||||
        self.assertFalse(player.move_up())
 | 
			
		||||
        self.assertTrue(player.move_left())
 | 
			
		||||
        self.assertFalse(player.move_left())
 | 
			
		||||
        for i in range(8):
 | 
			
		||||
            self.assertTrue(player.move_down())
 | 
			
		||||
        self.assertFalse(player.move_down())
 | 
			
		||||
        self.assertTrue(player.move_right())
 | 
			
		||||
        self.assertTrue(player.move_right())
 | 
			
		||||
        self.assertTrue(player.move_right())
 | 
			
		||||
        self.assertFalse(player.move_right())
 | 
			
		||||
        self.assertTrue(player.move_down())
 | 
			
		||||
        self.assertTrue(player.move_down())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user