Skip to content

Commit

Permalink
#56: Factored a generic Entity class out of the Mobile class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ecdavis committed Sep 23, 2016
1 parent 2a672cb commit 05fceb8
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 104 deletions.
1 change: 1 addition & 0 deletions spacegame/core/aux_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AUX_TYPE_ENTITY = "aux.entity"
2 changes: 1 addition & 1 deletion spacegame/core/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def close_brain_hook(_, brain):
mobile = brain.mobile
mobile.detach_brain()
if pantsmud.game.environment:
pantsmud.game.environment.remove_mobile(mobile)
pantsmud.game.environment.remove_entity(mobile)
if brain.identity:
identity = brain.identity
identity.detach_brain()
Expand Down
6 changes: 4 additions & 2 deletions spacegame/modules/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ def chat_global_command(brain, cmd, args):
universe = mobile.universe
data = {"mobile_from": mobile.name, "message": params["message"]}
message.command_success(mobile, cmd, data)
for m in (universe.mobiles[u] for u in universe.mobiles if u is not mobile.uuid):
for m in universe.get_mobiles():
if m is mobile:
continue
message.notify(m, cmd, data)


def chat_private_command(brain, cmd, args):
params = parser.parse([("mobile_name", parser.WORD), ("message", parser.STRING)], args)
mobile = brain.mobile
target = mobile.universe.get_mobile(params["mobile_name"])
target = mobile.universe.get_entity(params["mobile_name"])
if not target:
raise error.CommandFail() # TODO Add error message.
if target is mobile:
Expand Down
2 changes: 1 addition & 1 deletion spacegame/modules/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def quit_command(brain, _, args):
def shutdown_command(brain, _, args):
parser.parse([], args)
universe = pantsmud.game.environment
for m in [universe.mobiles[u] for u in universe.mobiles]:
for m in universe.get_mobiles():
if m.brain.identity:
user.save_player(m)
m.brain.close()
Expand Down
2 changes: 1 addition & 1 deletion spacegame/modules/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def login_command(brain, cmd, args):
pantsmud.game.environment.add_identity(u)
p = user.load_player(u.player_uuid)
p.attach_brain(brain)
pantsmud.game.environment.add_mobile(p)
pantsmud.game.environment.add_entity(p)
message.command_success(brain, cmd, {"name": p.name})
brain.replace_input_handler(command.command_input_handler, "game")

Expand Down
2 changes: 1 addition & 1 deletion spacegame/modules/thrust.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def position_update(mobile, seconds):


def position_update_cycle():
for mobile in pantsmud.game.environment.mobiles.values():
for mobile in pantsmud.game.environment.entities.values():
position_update(mobile, POSITION_UPDATE_TICK)


Expand Down
4 changes: 2 additions & 2 deletions spacegame/modules/warp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pantsmud.game
from pantsmud.driver import auxiliary, command, hook, parser
from pantsmud.util import error, message
from spacegame.core import hook_types
from spacegame.core import aux_types, hook_types


class WarpAux(object):
Expand Down Expand Up @@ -56,7 +56,7 @@ def clear_warp_scanner(_, mobile):


def init():
auxiliary.install(auxiliary.AUX_TYPE_MOBILE, "warp", WarpAux)
auxiliary.install(aux_types.AUX_TYPE_ENTITY, "warp", WarpAux)
command.add_command("warp", warp_command)
command.add_command("warp.scan", warp_scan_command)
command.add_command("warp.scan.activate", warp_scan_activate_command)
Expand Down
89 changes: 89 additions & 0 deletions spacegame/universe/entity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import uuid
from pantsmud.driver import auxiliary
from spacegame.core import aux_types


class Entity(object):
"""
A representation of an entity in the game universe.
"""
def __init__(self):
self.uuid = uuid.uuid4()
self.name = ""
self.universe = None
self.celestial_uuid = None
self.position = (0, 0, 0)
self.vector = (1.0, 0.0, 0.0)
self.speed = 0
self.aux = auxiliary.new_data(aux_types.AUX_TYPE_ENTITY)

def load_data(self, data):
"""
Loads a dictionary containing saved Entity data onto the object.
This method expects well-formed data. It will validate all fields and raise an exception if any of the data is
invalid.
Data layout:
{
"uuid": "<uuid>",
"name": "<word>",
"celestial_uuid": "<uuid>",
"auxiliary": <dict> # This will be passed to pantsmud.auxiliary.load_data
}
"""
self.uuid = uuid.UUID(data["uuid"])
self.name = data["name"]
self.celestial_uuid = uuid.UUID(data["celestial_uuid"]) # TODO This isn't safe if the Celestial no longer exists.
self.aux = auxiliary.load_data(self.aux, data["auxiliary"])

def save_data(self):
"""
Returns a dictionary containing Entity data ready to be serialized.
"""
return {
"uuid": str(self.uuid),
"name": self.name,
"celestial_uuid": str(self.celestial_uuid),
"auxiliary": auxiliary.save_data(self.aux)
}

@property
def celestial(self):
"""
Get the Entity's Celestial, if it has one.
"""
if self.celestial_uuid:
return self.universe.celestials[self.celestial_uuid]
else:
return self.celestial_uuid

@celestial.setter
def celestial(self, celestial):
"""
Set the Entity's Celestial.
"""
if celestial:
self.celestial_uuid = celestial.uuid
else:
self.celestial_uuid = None

@property
def star_system(self):
"""
Get the Entity's StarSystem.
This is the StarSystem of the Entity's Celestial, if it has one.
"""
if self.celestial:
return self.celestial.star_system
else:
return None

def velocity(self):
"""
Get the Entity's current velocity.
"""
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
85 changes: 3 additions & 82 deletions spacegame/universe/mobile.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@
import uuid
from pantsmud.driver import auxiliary
from spacegame.universe import entity


class Mobile(object):
class Mobile(entity.Entity):
"""
A representation of a mobile entity in the game universe.
"""
def __init__(self):
self.uuid = uuid.uuid4()
self.name = ""
self.universe = None
entity.Entity.__init__(self)
self.brain_uuid = None
self.celestial_uuid = None
self.position = (0, 0, 0)
self.vector = (1.0, 0.0, 0.0)
self.speed = 0
self.aux = auxiliary.new_data(auxiliary.AUX_TYPE_MOBILE)

def load_data(self, data):
"""
Loads a dictionary containing saved Mobile data onto the object.
This method expects well-formed data. It will validate all fields and raise an exception if any of the data is
invalid.
Data layout:
{
"uuid": "<uuid>",
"name": "<word>",
"celestial_uuid": "<uuid>",
"auxiliary": <dict> # This will be passed to pantsmud.auxiliary.load_data
}
"""
self.uuid = uuid.UUID(data["uuid"])
self.name = data["name"]
self.celestial_uuid = uuid.UUID(data["celestial_uuid"]) # TODO This isn't safe if the Celestial no longer exists.
self.aux = auxiliary.load_data(self.aux, data["auxiliary"])

def save_data(self):
"""
Returns a dictionary containing Mobile data ready to be serialized.
"""
return {
"uuid": str(self.uuid),
"name": self.name,
"celestial_uuid": str(self.celestial_uuid),
"auxiliary": auxiliary.save_data(self.aux)
}

@property
def environment(self):
Expand Down Expand Up @@ -91,46 +52,6 @@ def detach_brain(self):
self.brain.mobile = None
self.brain = None

@property
def celestial(self):
"""
Get the Mobile's Celestial, if it has one.
"""
if self.celestial_uuid:
return self.universe.celestials[self.celestial_uuid]
else:
return self.celestial_uuid

@celestial.setter
def celestial(self, celestial):
"""
Set the Mobile's Celestial.
"""
if celestial:
self.celestial_uuid = celestial.uuid
else:
self.celestial_uuid = None

@property
def star_system(self):
"""
Get the Mobile's StarSystem.
This is the StarSystem of the Mobile's Celestial, if it has one.
"""
if self.celestial:
return self.celestial.star_system
else:
return None

def velocity(self):
"""
Get the Mobile's current velocity.
"""
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 message(self, name, data=None):
"""
Send a message to the Mobile's Brain, if it has one.
Expand Down
34 changes: 20 additions & 14 deletions spacegame/universe/universe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import uuid
from pantsmud.driver import auxiliary
from spacegame.universe import mobile


class Universe(object):
Expand All @@ -13,7 +14,7 @@ def __init__(self):
self.sessions = set()
self.brains = {}
self.identities = {}
self.mobiles = {}
self.entities = {}
self.star_systems = {}
self.celestials = {}
self.core_star_system_uuids = set()
Expand Down Expand Up @@ -89,28 +90,33 @@ def remove_identity(self, identity):
del self.identities[identity.uuid]
identity.universe = None

def get_mobile(self, mobile_name):
def get_entity(self, entity_name):
"""
Get a Mobile by name.
Get a Entity by name.
"""
for mobile in self.mobiles.itervalues():
if mobile.name == mobile_name:
return mobile
for entity in self.entities.itervalues():
if entity.name == entity_name:
return entity
return None

def add_mobile(self, mobile):
def add_entity(self, entity):
"""
Add a Mobile to the Universe.
Add a Entity to the Universe.
"""
mobile.universe = self
self.mobiles[mobile.uuid] = mobile
entity.universe = self
self.entities[entity.uuid] = entity

def remove_mobile(self, mobile):
def remove_entity(self, entity):
"""
Remove a Mobile from the Universe.
Remove a Entity from the Universe.
"""
del self.mobiles[mobile.uuid]
mobile.universe = None
del self.entities[entity.uuid]
entity.universe = None

def get_mobiles(self):
mobiles = self.entities.values()
mobiles = filter(lambda e: isinstance(e, mobile.Mobile), mobiles) # TODO Make it a flag
return mobiles

def get_star_system(self, star_system_name):
"""
Expand Down

0 comments on commit 05fceb8

Please sign in to comment.