Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 8 commits
  • 13 files changed
  • 0 commit comments
  • 1 contributor
View
10 data.py
@@ -9,13 +9,19 @@
def get_node_text(node, strip_newlines=True): #get all the text from a node
texts = []
for n in node.childNodes: #loop through children
- if n.nodeType == node.TEXT_NODE: #if it's text
- texts.append(node.data) #store its value
+ if n.nodeType == n.TEXT_NODE: #if it's text
+ texts.append(n.data) #store its value
texts = "".join(texts) #combine all the text together
if strip_newlines: #if we're supposed to remove newlines
return texts.replace("\r", "").replace("\n", "") #do so
return texts #return combined text
+def get_xml_prop(root, name, strip_newlines=True): #get the text from a "property" node with the given name
+ try:
+ return get_node_text(root.getElementsByTagName(name)[0], strip_newlines)
+ except:
+ return None
+
def get_path(path, with_data=True): #convert a path to one appropriate for the host with the data directory
path = path.replace("\\", "/") #convert backslashes to forward slashes
if with_data: #if we're prepending the data directory
View
17 data/maps/cave.tmx
@@ -1,9 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="20" height="20" tilewidth="16" tileheight="16">
- <properties>
- <property name="id" value="cave"/>
- <property name="object_data" value="maps/cave_objects.xml"/>
- </properties>
<tileset firstgid="1" name="cavebackground" tilewidth="16" tileheight="16">
<image source="../tilesets/cavebackground.png" trans="808080" width="32" height="32"/>
</tileset>
@@ -48,19 +44,6 @@
</data>
</layer>
<objectgroup name="Objects" width="20" height="20">
- <object name="player" type="Player" x="48" y="206" width="16" height="16">
- <properties>
- <property name="id" value="player"/>
- </properties>
- </object>
- <object name="cave_indoor_warp" type="warp" x="315" y="-64">
- <properties>
- <property name="dest_map" value="oasis_inside.tmx"/>
- <property name="dest_warp" value="indoor_cave_warp"/>
- <property name="id" value="cave_indoor_warp"/>
- <property name="tile_pos" value="10,8"/>
- </properties>
- </object>
</objectgroup>
<layer name="Tile Layer 7" width="20" height="20">
<data encoding="base64" compression="zlib">
View
6 data/maps/cave.xml
@@ -0,0 +1,6 @@
+<map id="cave" tiles="maps/cave.tmx">
+ <object id="cave_indoor_warp" type="warp" pos="10,8">
+ <dest_map>oasis_inside.xml</dest_map>
+ <dest_warp>indoor_cave_warp</dest_warp>
+ </object>
+</map>
View
2 data/maps/cave_objects.xml
@@ -1,2 +0,0 @@
-<objects>
-</objects>
View
30 data/maps/oasis.tmx
@@ -1,9 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="36" height="21" tilewidth="16" tileheight="16">
- <properties>
- <property name="id" value="oasis"/>
- <property name="object_data" value="maps/oasis_objects.xml"/>
- </properties>
<tileset firstgid="1" name="Grass" tilewidth="16" tileheight="16">
<image source="../tilesets/hgss_tileset_terrain_and_paths_by_thunderdove-d3bnn5w.png" trans="c0c0c0" width="336" height="160"/>
</tileset>
@@ -40,32 +36,6 @@
</data>
</layer>
<objectgroup name="NPC" width="36" height="21">
- <object name="player" type="Player" x="248" y="256" width="16" height="16">
- <properties>
- <property name="id" value="player"/>
- </properties>
- </object>
- <object name="house_warp" type="warp" x="446" y="-94">
- <properties>
- <property name="dest_map" value="oasis_inside.tmx"/>
- <property name="dest_warp" value="in_house_warp"/>
- <property name="id" value="house_warp"/>
- <property name="tile_pos" value="28,10"/>
- </properties>
- </object>
- <object name="oasis_sign" type="sign" x="490" y="-10" width="48" height="9">
- <properties>
- <property name="id" value="oasis_sign"/>
- <property name="text" value="Welcome to the Testing Oasis.{br}Enjoy your stay!{wait}"/>
- <property name="tile_pos" value="26,13"/>
- </properties>
- </object>
- <object name="test_npc" type="npc" x="0" y="0">
- <properties>
- <property name="id" value="test_npc"/>
- <property name="tile_pos" value="5,9"/>
- </properties>
- </object>
</objectgroup>
<layer name="house top" width="36" height="21">
<data encoding="base64" compression="zlib">
View
11 data/maps/oasis_objects.xml → data/maps/oasis.xml
@@ -1,9 +1,16 @@
-<map>
+<map id="oasis" tiles="maps/oasis.tmx">
<wild for="grass">
<pokemon type="zubat" level="6-8|12" />
<pokemon type="geodude" level="4-5|7" rarity="2" />
</wild>
- <object id="test_npc">
+ <object id="oasis_sign" type="sign" pos="26,13">
+ <text>Welcome to the Testing Oasis.{br}Enjoy your stay!{wait}</text>
+ </object>
+ <object id="house_warp" type="warp" pos="28,10">
+ <dest_map>oasis_inside.xml</dest_map>
+ <dest_warp>in_house_warp</dest_warp>
+ </object>
+ <object id="test_npc" type="npc" pos="5,9">
<anim file="objects/player/player_animation.xml" />
<movement speed="2">
<move dir="up" dist="4" />
View
31 data/maps/oasis_inside.tmx
@@ -1,9 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="20" height="20" tilewidth="16" tileheight="16">
- <properties>
- <property name="id" value="oasis_inside"/>
- <property name="object_data" value="maps/oasis_inside_objects.xml"/>
- </properties>
<tileset firstgid="1" name="maptypetileset" tilewidth="16" tileheight="16">
<image source="../tilesets/maptypetileset.png" width="32" height="64"/>
</tileset>
@@ -16,33 +12,6 @@
</data>
</layer>
<objectgroup name="objs" width="20" height="20">
- <object name="player" type="Player" x="274" y="-68">
- <properties>
- <property name="id" value="player"/>
- </properties>
- </object>
- <object name="house_inside" type="warp" x="183" y="-68">
- <properties>
- <property name="dest_map" value="oasis.tmx"/>
- <property name="dest_warp" value="house_warp"/>
- <property name="id" value="in_house_warp"/>
- <property name="tile_pos" value="10,18"/>
- </properties>
- </object>
- <object name="test_npc" type="npc" x="0" y="0">
- <properties>
- <property name="id" value="test_npc2"/>
- <property name="tile_pos" value="3,3"/>
- </properties>
- </object>
- <object name="cave_warp" type="warp" x="67" y="-59">
- <properties>
- <property name="dest_map" value="cave.tmx"/>
- <property name="dest_warp" value="cave_indoor_warp"/>
- <property name="id" value="indoor_cave_warp"/>
- <property name="tile_pos" value="17,2"/>
- </properties>
- </object>
</objectgroup>
<layer name="Collisions" width="20" height="20">
<data encoding="base64" compression="zlib">
View
22 data/maps/oasis_inside.xml
@@ -0,0 +1,22 @@
+<map id="oasis_inside" tiles="maps/oasis_inside.tmx">
+ <object id="test_npc2" type="npc" pos="3,3">
+ <anim file="objects/player/player_animation.xml" />
+ <movement speed="2">
+ <move dir="right" dist="10" />
+ <move dir="down" dist="10" />
+ <move dir="left" dist="10" />
+ <move dir="up" dist="10" />
+ </movement>
+ <script>
+ <dialog>Wait, what? A third?{br}Why are there so many...?{wait}</dialog>
+ </script>
+ </object>
+ <object id="in_house_warp" type="warp" pos="10,18">
+ <dest_map>oasis.xml</dest_map>
+ <dest_warp>house_warp</dest_warp>
+ </object>
+ <object id="indoor_cave_warp" type="warp" pos="17,2">
+ <dest_map>cave.xml</dest_map>
+ <dest_warp>cave_indoor_warp</dest_warp>
+ </object>
+</map>
View
14 data/maps/oasis_inside_objects.xml
@@ -1,14 +0,0 @@
-<objects>
- <object id="test_npc2">
- <anim file="objects/player/player_animation.xml" />
- <movement speed="2">
- <move dir="right" dist="10" />
- <move dir="down" dist="10" />
- <move dir="left" dist="10" />
- <move dir="up" dist="10" />
- </movement>
- <script>
- <dialog>Wait, what? A third?{br}Why are there so many...?{wait}</dialog>
- </script>
- </object>
-</objects>
View
41 game.py
@@ -13,8 +13,6 @@
import battle
import data
-import animation
-
class Game: #class for our game engine
def __init__(self, g):
self.g = g #store global variables
@@ -34,30 +32,34 @@ def __init__(self, g):
self.dialog_drawing = False #set when the dialog is showing text
self.dialog_result = None #hold result of a dialog
self.dialog_callback = None #callback for dialog completion
- self.object_data = {} #dictionary of loaded object data
self.debug = False #whether we're in debug mode or not
self.menu = menu.Menu(self) #initialize a menu manager
self.menu_showing = False #whether the menu is being shown
def start(self):
self.player = player.Player(self) #initialize a player object
- self.load_map(self.g.save.get_game_prop("game", "curr_map", "maps/oasis.tmx")) #load map
+ self.load_map(self.g.save.get_game_prop("game", "curr_map", "maps/oasis.xml")) #load map
self.map_image = self.map.update() #update it once
self.transition(transition.FadeIn(32)) #start fade in
- self.pa = animation.PartAnimationSet(self.g, "part_animation_test.xml")
- self.pa.set_animation("demo")
def load_map(self, map_file): #load a map
- self.map = map.Map(self.g, map_file) #load the map
+ map_dom = data.load_xml(map_file).documentElement #load map xml data
+ tile_file = map_dom.getAttribute("tiles") #get tile map file
+ self.map = map.Map(self.g, tile_file) #load the map
self.map_file = map_file #save map file
- objects_dom = data.load_xml(self.map.properties["object_data"]).documentElement #parse object data file
- self.object_data = {} #clear previous object data
self.wild_pokemon = {} #clear wild pokemon data too
- child = objects_dom.firstChild #get first object data element
+ child = map_dom.firstChild #get first object data element
while child is not None: #loop through object data
if child.localName == "object": #if it's an object element
- self.object_data[child.getAttribute("id")] = child #store it under its id
+ obj_id = child.getAttribute("id") #get its id
+ obj_type = child.getAttribute("type") #and type
+ #initialize new object
+ obj = objects.obj_types[obj_type](self, child)
+ self.objects[obj_id] = obj #store it
+ self.map.add_object(obj) #add it to map
elif child.localName == "wild": #if it's wild pokemon data
self.parse_wild(child) #handle it
child = child.nextSibling #go to next element
+ self.objects["player"] = self.player #store player object
+ self.map.add_object(self.player) #and add it to map
def parse_wild(self, wild): #parse wild pokemon data
when = wild.getAttribute("for") #get when the data will be used
data = []
@@ -79,17 +81,6 @@ def parse_wild(self, wild): #parse wild pokemon data
for x in xrange(rarity): #add it once for each rarity
data.append(t)
self.wild_pokemon[when] = data #add generated list to wild data
- def add_object(self, obj_node): #add an object
- properties = {} #dictionary of the object's properties
- for property in obj_node.getElementsByTagName("property"): #get properties
- properties[property.getAttribute("name")] = property.getAttribute("value") #get a property
- type = obj_node.getAttribute("type") #get type of object
- if type == "Player": #if we're trying to load a player object
- obj = self.player #return it
- else: #otherwise
- obj = objects.obj_types[type](self, obj_node, properties) #load the object
- self.objects[properties["id"]] = obj #store it
- return obj #and return it
def add_warp(self, pos, obj): #add a warp object
self.warps[pos] = obj #store the warp
def prepare_warp(self, pos): #prepare a warp
@@ -112,9 +103,8 @@ def perform_warp(self): #actually warp
self.load_map(map_file) #load the map
new_warp = self.objects[warp_obj["dest_warp"]] #get the warp destination
player = self.objects["player"] #get the player object
- new_pos = new_warp.properties["tile_pos"].split(",") #calculate warp destination
- new_pos = (int(new_pos[0].strip()), int(new_pos[1].strip()))
- player.tile_pos = new_pos #set player position
+ new_pos = (new_warp.tile_x, new_warp.tile_y) #get warp destination
+ player.tile_pos = new_pos[:] #set player position
player.pos = [((player.tile_pos[0]-1)*16)+8, (player.tile_pos[1]-1)*16]
player.rect = pygame.Rect(player.pos, player.size)
self.map_image = self.map.update() #update map once
@@ -222,7 +212,6 @@ def update(self): #update the engine for this frame
pygame.draw.polygon(self.surf, (161, 161, 161), [[64, 44], [pos[0]+2, pos[1]+2], [82, 44]])
pygame.draw.polygon(self.surf, (255, 255, 255), [[64, 43], pos, [80, 43]])
if self.debug: self.font.render(str(self.g.fps), self.surf, (0, 180)) #draw framerate
- if self.debug: self.pa.update(self.surf, 50, 50)
return self.surf #return the rendered surface
def save(self, fname=None): #save our data
for id in self.objects: #loop through all our objects
View
3 map.py
@@ -92,9 +92,6 @@ def __init__(self, g, map, layer_node):
self.g = g #store globals
self.map = map
self.objects = [] #list of objects on this layer
- for object in layer_node.getElementsByTagName("object"): #load all objects
- obj = self.g.game.add_object(object) #load the object
- self.objects.append(obj) #save it to the object list
self.map.obj_layer = self #store ourselves in the map
def add_object(self, obj): #add an object to the render list
self.objects.append(obj)
View
41 objects.py
@@ -192,20 +192,26 @@ def __init__(self):
self.shadow = data.load_image("objects/npcshadow.png") #load shadow image
self.shadow.convert_alpha() #convert the image for faster drawing
def draw(self, surf): #draw ourselves onto a given surface
- surf.blit(self.shadow, (self.rect.x+8, self.rect.y+25)) #draw shadow
+ surf.blit(self.shadow, (self.rect.x+8, self.rect.y+24)) #draw shadow
RenderedObject.draw(self, surf) #call renderer for parent class
#warp point object
class Warp:
- def __init__(self, game, element, properties):
+ def __init__(self, game, element):
self.g = game.g #store parameters
self.game = game
- self.properties = properties
+ properties = {}
#get tile we're monitoring
- t = self.properties["tile_pos"].split(",")
+ t = element.getAttribute("pos").split(",")
self.tile_x = int(t[0].strip())
self.tile_y = int(t[1].strip())
- game.add_warp((self.tile_x, self.tile_y), self.properties) #add the warp
+ #load destination properties
+ dest_warp = data.get_xml_prop(element, "dest_warp")
+ dest_map = data.get_xml_prop(element, "dest_map")
+ if dest_warp is not None: #if there is a destination
+ properties["dest_warp"] = dest_warp #store properties
+ properties["dest_map"] = dest_map
+ game.add_warp((self.tile_x, self.tile_y), properties) #add the warp
self.visible = False #we're not rendering anything
def interact(self, pos):
pass #don't interact
@@ -216,11 +222,11 @@ def save(self): #we don't need to save anything
#sign object
class Sign:
- def __init__(self, game, element, properties):
+ def __init__(self, game, element):
self.game = game #store parameters
- self.text = properties["text"] #store text to show
+ self.text = data.get_xml_prop(element, "text") #store text to show
#get our tile position
- t = properties["tile_pos"].split(",")
+ t = element.getAttribute("pos").split(",")
self.tile_pos = (int(t[0].strip()), int(t[1].strip())) #store position
game.set_obj_pos(self, self.tile_pos) #set our position
self.visible = False #we're not rendering anything
@@ -233,32 +239,24 @@ def save(self): #we don't need to save anything
#generic NPC
class NPC(RenderedNPC):
- def __init__(self, game, element, properties):
+ def __init__(self, game, element):
RenderedNPC.__init__(self) #init the renderer class
- self.properties = properties #store parameters
self.game = game
- t = properties["tile_pos"].split(",") #load tile position
+ t = element.getAttribute("pos").split(",") #load tile position
self.tile_pos = [int(t[0].strip()), int(t[1].strip())]
self.pos = [((self.tile_pos[0]-1)*16), (self.tile_pos[1]-1)*16] #set real position
- self.image = None #we have no image for now
- self.inited = False #mark that we haven't been initialized fully yet
self.interacting = False #mark that we're not interacting
self.visible = True #and we should be showing ourselves
- def do_init(self): #perform initialization on first update
- self.inited = True #we've initialized now
- properties = self.properties #load properties
- game = self.game #and game object
- self.obj_data = game.object_data[properties["id"]] #get our associated data
#load an animator
- self.animator = animation.AnimationGroup(game.g, self, self.obj_data.getElementsByTagName("anim")[0].getAttribute("file"))
+ self.animator = animation.AnimationGroup(game.g, self, element.getElementsByTagName("anim")[0].getAttribute("file"))
self.animator.set_animation("stand_down") #set animation
self.animator.update() #update animation once
#and a movement manager
self.move_manager = MovementManager(self)
#load movement list
- self.move_manager.load_move_dom(self.obj_data.getElementsByTagName("movement")[0])
+ self.move_manager.load_move_dom(element.getElementsByTagName("movement")[0])
self.script_manager = script.Script(self) #initialize script manager
- self.interaction_script = self.obj_data.getElementsByTagName("script")[0] #load script
+ self.interaction_script = element.getElementsByTagName("script")[0] #load script
def interact(self, pos):
#make ourselves face to who's talking
new_pos = [1, 0, 3, 2][pos]
@@ -267,7 +265,6 @@ def interact(self, pos):
self.interacting = True #we're currently interacting
self.script_manager.start_script(self.interaction_script) #start interaction script
def update(self):
- if not self.inited: self.do_init() #intialize if we haven't already
if not self.interacting: #if we aren't interacting
if self.game.dialog_drawing: return #return if a dialog is being drawn
self.move_manager.update() #update our movement
View
9 script.py
@@ -2,6 +2,7 @@
from pygame.locals import *
import settings #load settings manager
+import data
#class for object scripts
class Script:
@@ -15,12 +16,6 @@ def start_script(self, s): #start a script running
self.dlog_wait = False #mark whether we're waiting for a dialog
self.callstack = [] #callstack for command execution
self.vars = {} #dictionary of variables for the current script
- def get_text(self, node): #get text from an element
- nodes = [] #list of found text nodes
- for n in node.childNodes: #loop through text
- if n.nodeType == n.TEXT_NODE: #if it's a text node
- nodes.append(n.data) #add it to the list of text
- return "".join(nodes) #return combined string
def get_var(self, var): #parse a variable string and return its value
try: #try to parse it as a variable
type = var[0] #get parts
@@ -38,7 +33,7 @@ def dialog_cb(self, result): #callback for dialog completion
self.vars["dlog_result"] = result #store result in variables
def cmd_dialog(self, cmd): #handle command
self.dlog_wait = True #we're waiting for a dialog
- self.obj.game.show_dlog(self.get_text(cmd), self.obj, callback=self.dialog_cb) #show the dialog
+ self.obj.game.show_dlog(data.get_node_text(cmd), self.obj, callback=self.dialog_cb) #show the dialog
def cmd_if(self, cmd): #handle if command
#get parameters
left = self.get_var(cmd.getAttribute("left"))

No commit comments for this range

Something went wrong with that request. Please try again.