Merged master into display (settings added)
This commit is contained in:
commit
cf70c2d1b7
|
@ -8,3 +8,6 @@ venv/
|
||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
|
|
||||||
__pycache__
|
__pycache__
|
||||||
|
|
||||||
|
# Don't commit settings
|
||||||
|
settings.json
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from .interfaces import Map
|
from .interfaces import Map
|
||||||
from .mapdisplay import MapDisplay
|
from .mapdisplay import MapDisplay
|
||||||
|
from .settings import Settings
|
||||||
from .term_manager import TermManager
|
from .term_manager import TermManager
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,6 +9,9 @@ class Game:
|
||||||
|
|
||||||
def init(self) -> None:
|
def init(self) -> None:
|
||||||
Game.INSTANCE = self
|
Game.INSTANCE = self
|
||||||
|
self.settings = Settings()
|
||||||
|
self.settings.load_settings()
|
||||||
|
self.settings.write_settings()
|
||||||
self.key_handler = self.player_move
|
self.key_handler = self.player_move
|
||||||
|
|
||||||
def new_game(self):
|
def new_game(self):
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
#!/usr/bin/env python
|
|
||||||
import curses
|
|
||||||
from dungeonbattle.interfaces import Map
|
|
||||||
|
|
||||||
class MapDisplay:
|
|
||||||
|
|
||||||
def __init__(self, m: Map):
|
|
||||||
self.map = m
|
|
||||||
self.pad = curses.newpad(m.height, m.width+1)
|
|
||||||
|
|
||||||
def update_pad(self):
|
|
||||||
self.pad.addstr(0, 0, self.map.draw_string())
|
|
||||||
for e in self.map.entities:
|
|
||||||
self.pad.addch(e.y, e.x, e.img)
|
|
||||||
|
|
||||||
def display(self, y, x):
|
|
||||||
deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS //2) + 1
|
|
||||||
pminrow, pmincol = y-deltay, x-deltax
|
|
||||||
sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0)
|
|
||||||
deltay, deltax = curses.LINES - deltay, curses.COLS - deltax
|
|
||||||
smaxrow = self.map.height - (y + deltay) + curses.LINES -1
|
|
||||||
smaxrow = min(smaxrow, curses.LINES-1)
|
|
||||||
smaxcol = self.map.width - (x + deltax) + curses.COLS -1
|
|
||||||
smaxcol = min(smaxcol, curses.COLS-1)
|
|
||||||
pminrow = max(0, min(self.map.height, pminrow))
|
|
||||||
pmincol = max(0, min(self.map.width, pmincol))
|
|
||||||
self.pad.clear()
|
|
||||||
self.update_pad()
|
|
||||||
self.pad.refresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol)
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from typing import Any, Generator
|
||||||
|
|
||||||
|
|
||||||
|
class Settings:
|
||||||
|
"""
|
||||||
|
This class stores the settings of the game.
|
||||||
|
Settings can be get by using for example settings.TEXTURE_PACK directly.
|
||||||
|
The comment can be get by using settings.get_comment('TEXTURE_PACK').
|
||||||
|
We can define the setting by simply use settings.TEXTURE_PACK = 'new_key'
|
||||||
|
"""
|
||||||
|
def __init__(self):
|
||||||
|
self.KEY_UP_PRIMARY = ['z', 'Touche principale pour aller vers le haut']
|
||||||
|
self.KEY_UP_SECONDARY = ['KEY_UP', 'Touche secondaire pour aller vers le haut']
|
||||||
|
self.KEY_DOWN_PRIMARY = ['s', 'Touche principale pour aller vers le bas']
|
||||||
|
self.KEY_DOWN_SECONDARY = ['KEY_DOWN', 'Touche secondaire pour aller vers le bas']
|
||||||
|
self.KEY_LEFT_PRIMARY = ['q', 'Touche principale pour aller vers la gauche']
|
||||||
|
self.KEY_LEFT_SECONDARY = ['KEY_LEFT', 'Touche secondaire pour aller vers la gauche']
|
||||||
|
self.KEY_RIGHT_PRIMARY = ['d', 'Touche principale pour aller vers la droite']
|
||||||
|
self.KEY_RIGHT_SECONDARY = ['KEY_RIGHT', 'Touche secondaire pour aller vers la droite']
|
||||||
|
self.TEXTURE_PACK = ['ASCII', 'Pack de textures utilisé']
|
||||||
|
|
||||||
|
def __getattribute__(self, item: str) -> Any:
|
||||||
|
superattribute = super().__getattribute__(item)
|
||||||
|
if item.isupper() and item in self.settings_keys:
|
||||||
|
return superattribute[0]
|
||||||
|
return superattribute
|
||||||
|
|
||||||
|
def __setattr__(self, name: str, value: Any) -> None:
|
||||||
|
if name in self.settings_keys:
|
||||||
|
object.__getattribute__(self, name)[0] = value
|
||||||
|
return
|
||||||
|
return super().__setattr__(name, value)
|
||||||
|
|
||||||
|
def get_comment(self, item: str) -> str:
|
||||||
|
"""
|
||||||
|
Retrieve the comment of a setting.
|
||||||
|
"""
|
||||||
|
if item in self.settings_keys:
|
||||||
|
return object.__getattribute__(self, item)[1]
|
||||||
|
for key in self.settings_keys:
|
||||||
|
if getattr(self, key) == item:
|
||||||
|
return object.__getattribute__(self, key)[1]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def settings_keys(self) -> Generator[str, Any, None]:
|
||||||
|
"""
|
||||||
|
Get the list of all parameters.
|
||||||
|
"""
|
||||||
|
return (key for key in self.__dict__)
|
||||||
|
|
||||||
|
def loads_from_string(self, json_str: str) -> None:
|
||||||
|
"""
|
||||||
|
Dump settings
|
||||||
|
"""
|
||||||
|
d = json.loads(json_str)
|
||||||
|
for key in d:
|
||||||
|
setattr(self, key, d[key])
|
||||||
|
|
||||||
|
def dumps_to_string(self) -> str:
|
||||||
|
"""
|
||||||
|
Dump settings
|
||||||
|
"""
|
||||||
|
d = dict()
|
||||||
|
for key in self.settings_keys:
|
||||||
|
d[key] = getattr(self, key)
|
||||||
|
return json.dumps(d, indent=4)
|
||||||
|
|
||||||
|
def load_settings(self) -> None:
|
||||||
|
"""
|
||||||
|
Loads the settings from a file
|
||||||
|
"""
|
||||||
|
if os.path.isfile("settings.json"):
|
||||||
|
with open("settings.json", "r") as f:
|
||||||
|
self.loads_from_string(f.read())
|
||||||
|
|
||||||
|
def write_settings(self) -> None:
|
||||||
|
"""
|
||||||
|
Dumps the settings into a file
|
||||||
|
"""
|
||||||
|
with open("settings.json", "w") as f:
|
||||||
|
f.write(self.dumps_to_string())
|
|
@ -0,0 +1,30 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from dungeonbattle.settings import Settings
|
||||||
|
|
||||||
|
|
||||||
|
class TestSettings(unittest.TestCase):
|
||||||
|
def test_settings(self) -> None:
|
||||||
|
"""
|
||||||
|
Ensure that settings are well loaded.
|
||||||
|
"""
|
||||||
|
settings = Settings()
|
||||||
|
self.assertEqual(settings.KEY_UP_PRIMARY, 'z')
|
||||||
|
self.assertEqual(settings.KEY_DOWN_PRIMARY, 's')
|
||||||
|
self.assertEqual(settings.KEY_LEFT_PRIMARY, 'q')
|
||||||
|
self.assertEqual(settings.KEY_RIGHT_PRIMARY, 'd')
|
||||||
|
self.assertEqual(settings.KEY_UP_SECONDARY, 'KEY_UP')
|
||||||
|
self.assertEqual(settings.KEY_DOWN_SECONDARY, 'KEY_DOWN')
|
||||||
|
self.assertEqual(settings.KEY_LEFT_SECONDARY, 'KEY_LEFT')
|
||||||
|
self.assertEqual(settings.KEY_RIGHT_SECONDARY, 'KEY_RIGHT')
|
||||||
|
self.assertEqual(settings.TEXTURE_PACK, 'ASCII')
|
||||||
|
self.assertEqual(settings.get_comment(settings.TEXTURE_PACK), settings.get_comment('TEXTURE_PACK'))
|
||||||
|
self.assertEqual(settings.get_comment(settings.TEXTURE_PACK), 'Pack de textures utilisé')
|
||||||
|
|
||||||
|
settings.TEXTURE_PACK = 'UNICODE'
|
||||||
|
self.assertEqual(settings.TEXTURE_PACK, 'UNICODE')
|
||||||
|
|
||||||
|
settings.write_settings()
|
||||||
|
settings.load_settings()
|
||||||
|
|
||||||
|
self.assertEqual(settings.TEXTURE_PACK, 'UNICODE')
|
Loading…
Reference in New Issue