Skip to content

Commit

Permalink
#56: Remove Mobile class. (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecdavis committed Oct 11, 2016
1 parent 2ba016e commit 0d1ce6d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 87 deletions.
6 changes: 3 additions & 3 deletions spacegame/core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions spacegame/modules/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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))
Expand Down
55 changes: 55 additions & 0 deletions spacegame/universe/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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()
60 changes: 0 additions & 60 deletions spacegame/universe/mobile.py

This file was deleted.

3 changes: 1 addition & 2 deletions spacegame/universe/universe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import uuid
from pantsmud.driver import auxiliary
from spacegame.universe import mobile


class Universe(object):
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -13,7 +12,6 @@ def get_unit_tests():
ApplicationUnitTestCase,
CelestialUnitTestCase,
EntityUnitTestCase,
MobileUnitTestCase,
PathConfigUnitTestCase,
StarSystemUnitTestCase,
UniverseUnitTestCase
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
18 changes: 0 additions & 18 deletions tests/unit/test_mobile.py

This file was deleted.

0 comments on commit 0d1ce6d

Please sign in to comment.