diff --git a/spacegame/core/user.py b/spacegame/core/user.py index 80c8be4..b143a5a 100644 --- a/spacegame/core/user.py +++ b/spacegame/core/user.py @@ -3,7 +3,7 @@ import uuid from pantsmud.util import convert, storage from spacegame import config -from spacegame.universe import mobile +from spacegame.universe import entity class User(object): @@ -78,7 +78,7 @@ def save_user(user): def player_name_exists(player_name): for filename in os.listdir(config.path.player_dir): - p = storage.load_file(os.path.join(config.path.player_dir, filename), mobile.Mobile) + p = storage.load_file(os.path.join(config.path.player_dir, filename), entity.Entity) if p.name == player_name: return True return False @@ -89,7 +89,7 @@ def player_exists(player_uuid): def load_player(player_uuid): - return storage.load_file(config.path.player_file_pattern % convert.uuid_to_base32(player_uuid), mobile.Mobile) + return storage.load_file(config.path.player_file_pattern % convert.uuid_to_base32(player_uuid), entity.Entity) def save_player(player): diff --git a/spacegame/modules/login.py b/spacegame/modules/login.py index 5b481c1..31ade01 100644 --- a/spacegame/modules/login.py +++ b/spacegame/modules/login.py @@ -4,7 +4,7 @@ from pantsmud.driver import command, parser from pantsmud.util import error, message from spacegame.core import login_manager, user -from spacegame.universe import mobile +from spacegame.universe import entity def register_command(brain, cmd, args): @@ -13,7 +13,7 @@ def register_command(brain, cmd, args): raise error.CommandFail() # TODO Add error message. u = user.User() u.name = params["name"] - p = mobile.Mobile() + p = entity.new_mobile() p.name = params["name"] star_system = random.choice(list(pantsmud.game.environment.core_star_systems)) p.celestial = random.choice(list(star_system.core_celestials)) diff --git a/spacegame/universe/entity.py b/spacegame/universe/entity.py index af8c132..ea34fd7 100644 --- a/spacegame/universe/entity.py +++ b/spacegame/universe/entity.py @@ -11,6 +11,7 @@ def __init__(self, is_warp_beacon=False): self.uuid = uuid.uuid4() self.name = "" self.universe = None + self.brain_uuid = None self.celestial_uuid = None self.position = (0, 0, 0) self.vector = (1.0, 0.0, 0.0) @@ -51,6 +52,56 @@ def save_data(self): "auxiliary": auxiliary.save_data(self.aux) } + @property + def environment(self): + """ + Get the Entity's Environment, if it has one. + """ + # TODO This is really hacky. Either rename Universe to World or reconsider PantsMUD's + # TODO on brains having a world attached. + return self.universe + + @property + def brain(self): + """ + Get the Entity's Brain, if it has one. + """ + if self.brain_uuid: + return self.universe.brains[self.brain_uuid] + else: + return self.brain_uuid + + @brain.setter + def brain(self, brain): + """ + Set the Entity's Brain. + """ + if brain: + self.brain_uuid = brain.uuid + else: + self.brain_uuid = None + + def attach_brain(self, brain): + """ + Attach a Brain to this Entity. + """ + self.brain = brain + brain.mobile = self + + def detach_brain(self): + """ + Detach a Brain from this Entity. + """ + self.brain.mobile = None + self.brain = None + + def message(self, name, data=None): + """ + Send a message to the Entity's Brain, if it has one. + """ + if self.brain: + self.brain.message(name, data) + @property def celestial(self): """ @@ -90,3 +141,7 @@ def velocity(self): return (round(self.vector[0]*self.speed, 3), # X round(self.vector[1]*self.speed, 3), # Y round(self.vector[2]*self.speed, 3)) # Z + + +def new_mobile(): + return Entity() diff --git a/spacegame/universe/mobile.py b/spacegame/universe/mobile.py deleted file mode 100644 index f12f074..0000000 --- a/spacegame/universe/mobile.py +++ /dev/null @@ -1,60 +0,0 @@ -from spacegame.universe import entity - - -class Mobile(entity.Entity): - """ - A representation of a mobile entity in the game universe. - """ - def __init__(self): - entity.Entity.__init__(self) - self.brain_uuid = None - - @property - def environment(self): - """ - Get the Mobile's Environment, if it has one. - """ - # TODO This is really hacky. Either rename Universe to World or reconsider PantsMUD's - # TODO on brains having a world attached. - return self.universe - - @property - def brain(self): - """ - Get the Mobile's Brain, if it has one. - """ - if self.brain_uuid: - return self.universe.brains[self.brain_uuid] - else: - return self.brain_uuid - - @brain.setter - def brain(self, brain): - """ - Set the Mobile's Brain. - """ - if brain: - self.brain_uuid = brain.uuid - else: - self.brain_uuid = None - - def attach_brain(self, brain): - """ - Attach a Brain to this Mobile. - """ - self.brain = brain - brain.mobile = self - - def detach_brain(self): - """ - Detach a Brain from this Mobile. - """ - self.brain.mobile = None - self.brain = None - - def message(self, name, data=None): - """ - Send a message to the Mobile's Brain, if it has one. - """ - if self.brain: - self.brain.message(name, data) diff --git a/spacegame/universe/universe.py b/spacegame/universe/universe.py index 97c1051..2cdc845 100644 --- a/spacegame/universe/universe.py +++ b/spacegame/universe/universe.py @@ -1,6 +1,5 @@ import uuid from pantsmud.driver import auxiliary -from spacegame.universe import mobile class Universe(object): @@ -119,7 +118,7 @@ def remove_entity(self, entity): def get_mobiles(self): mobiles = self.entities.values() - mobiles = filter(lambda e: isinstance(e, mobile.Mobile), mobiles) # TODO Make it a flag + mobiles = filter(lambda e: e.brain is not None, mobiles) # TODO Make it a flag return mobiles def get_star_system(self, star_system_name): diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py index bec9f8a..02630ae 100644 --- a/tests/unit/__init__.py +++ b/tests/unit/__init__.py @@ -3,7 +3,6 @@ from tests.unit.test_celestial import CelestialUnitTestCase from tests.unit.test_entity import EntityUnitTestCase from tests.unit.test_config import PathConfigUnitTestCase -from tests.unit.test_mobile import MobileUnitTestCase from tests.unit.test_star_system import StarSystemUnitTestCase from tests.unit.test_universe import UniverseUnitTestCase @@ -13,7 +12,6 @@ def get_unit_tests(): ApplicationUnitTestCase, CelestialUnitTestCase, EntityUnitTestCase, - MobileUnitTestCase, PathConfigUnitTestCase, StarSystemUnitTestCase, UniverseUnitTestCase diff --git a/tests/unit/test_entity.py b/tests/unit/test_entity.py index 4d8a227..770cb41 100644 --- a/tests/unit/test_entity.py +++ b/tests/unit/test_entity.py @@ -16,3 +16,16 @@ def test_celestial_getter_and_setter(self): def test_star_system_getter(self): entity = Entity() self.assertIsNone(entity.star_system) + + def test_brain_getter_and_setter(self): + brain = mock.MagicMock() + entity = Entity() + self.assertIsNone(entity.brain) + entity.brain = brain + self.assertEqual(entity.brain_uuid, brain.uuid) + entity.brain = None + self.assertIsNone(entity.brain) + + def test_environment_getter(self): + entity = Entity() + self.assertEqual(entity.environment, entity.universe) diff --git a/tests/unit/test_mobile.py b/tests/unit/test_mobile.py deleted file mode 100644 index 6a07691..0000000 --- a/tests/unit/test_mobile.py +++ /dev/null @@ -1,18 +0,0 @@ -import mock -from spacegame.universe.mobile import Mobile -from tests.unit.util import UnitTestCase - - -class MobileUnitTestCase(UnitTestCase): - def test_brain_getter_and_setter(self): - brain = mock.MagicMock() - mobile = Mobile() - self.assertIsNone(mobile.brain) - mobile.brain = brain - self.assertEqual(mobile.brain_uuid, brain.uuid) - mobile.brain = None - self.assertIsNone(mobile.brain) - - def test_environment_getter(self): - mobile = Mobile() - self.assertEqual(mobile.environment, mobile.universe)