Mercurial > fife-parpg
changeset 491:c4168eb47a44
Adding some comments to the shooter demo. Updated the copyright date and FIFE URL as well.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Thu, 06 May 2010 19:34:21 +0000 |
parents | 939a4dc12ca1 |
children | 16ceb3228324 |
files | demos/shooter/run.py demos/shooter/scripts/common/baseobject.py demos/shooter/scripts/common/eventlistenerbase.py demos/shooter/scripts/common/helpers.py demos/shooter/scripts/gui/guis.py demos/shooter/scripts/powerups.py demos/shooter/scripts/scene.py demos/shooter/scripts/ships/enemies.py demos/shooter/scripts/ships/player.py demos/shooter/scripts/ships/shipbase.py demos/shooter/scripts/soundmanager.py demos/shooter/scripts/weapons.py demos/shooter/scripts/world.py |
diffstat | 13 files changed, 311 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/demos/shooter/run.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/run.py Thu May 06 19:34:21 2010 +0000 @@ -3,8 +3,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or
--- a/demos/shooter/scripts/common/baseobject.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/common/baseobject.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or @@ -35,7 +35,19 @@ class SpaceObject(object): + """ + Space Object + + This is the base class for all game objects. + """ def __init__(self, scene, name, findInstance=True): + """ + @param scene A reference to the Scene + @param name The name of the space object + @param findInstance True if the instance you are looking for is already loaded + False if you want to load the instance yourself + + """ self._scene = scene self._model = self._scene.model self._layer = self._scene.objectlayer @@ -57,10 +69,17 @@ self._instnace = None def start(self): + """ + You must execute this function for the object to be updated + """ if self._instance: self._running = True def update(self): + """ + If the object is running this updates the FIFE instance location based on + the objects current velocity and time since last frame + """ if self._running: shiploc = self.location exactloc = shiploc.getExactLayerCoordinates() @@ -81,12 +100,25 @@ self.location = shiploc def stop(self): + """ + Stops the object from being updated. + """ self._running = False def destroy(self): + """ + You are meant to override this function to specify what happens when the object + gets destroyed + """ self._running = False def applyThrust(self, vector): + """ + Applies a thrust vector to the object. Note that objects do not have + mass or any inertia. + + @param vector A fife.DoublePoint() specifying the direction and intensity of thrust. + """ self._velocity.x += (vector.x * (self._scene.timedelta/1000.0))/self._xscale self._velocity.y += (vector.y * (self._scene.timedelta/1000.0))/self._yscale @@ -97,7 +129,11 @@ def applyBrake(self, brakingForce): - + """ + Applies a braking thrust in the opposite direction of the current velocity + + @param brakingForce a floating point value specifying how fast the object should decelerate + """ if self._velocity.length() <= .01: self._velocity.x = 0 self._velocity.y = 0 @@ -122,6 +158,10 @@ self._velocity.y += (norm.y * (self._scene.timedelta/1000.0))/self._yscale def removeFromScene(self): + """ + Queues this object to be removed from the scene. The scene will remove the object + next time the garbage collection routines are called. + """ self._scene.queueObjectForRemoval(self) def _isRunning(self): @@ -193,4 +233,4 @@ maxvelocity = property(_getMaxVelocity, _setMaxVelocity) running = property(_isRunning) changedposition = property(_changedPosition) - scenenodeid = property(_getNodeId, _setNodeId) \ No newline at end of file + scenenodeid = property(_getNodeId, _setNodeId)
--- a/demos/shooter/scripts/common/eventlistenerbase.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/common/eventlistenerbase.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or
--- a/demos/shooter/scripts/common/helpers.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/common/helpers.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or @@ -28,6 +28,10 @@ def normalize(vector): """ Helper function to normalize a vector + + @param vector a fife.DoublePoint() to be normalized + + @return A normalized fife.DoublePoint() """ norm = fife.DoublePoint(0,0) @@ -42,13 +46,30 @@ return norm class Rect(object): + """ + Rect + + A class used to specify the bounding box of objects. For use + with collision detection. + """ def __init__(self, x = 0, y = 0, w = 0, h = 0): + """ + @param x The x coordinate + @param y The y coordinate + @param w The width + @param h The height + """ self._x = x self._y = y self._w = w self._h = h def intersects(self, rect): + """ + Returns if the rect intersects with this Rect. + + @param rect the Rect to perform the test against + """ _x = self._x - rect.x; _y = self._y - rect.y; _w = self._w; @@ -103,6 +124,15 @@ h = property(_getH, _setH) def rotatePoint(origin, point, angle): + """ + Rotates a point around the specified origin. + + @param origin A fife.DoublePoint() specifying the origin + @param point A fife.DoublePoint() to be rotated + @param angle The angle in which to rotate the point + + @return A fife.DoublePoint() representing the rotated point + """ newp = fife.DoublePoint(0,0) theta = (angle * math.pi)/180 @@ -119,7 +149,19 @@ return newp class Timer(fife.TimeEvent): + """ + Timer + + This class wraps the fife.TimeEvent class to make it easily usable from Python + It allows for a TimeEvent to be executed once or multiple times. + """ def __init__(self,manager, delay=0,callback=None,repeat=0): + """ + @param manager The FIFE time manager + @param delay The delay in milliseconds to execute the callback + @param callback The function to execute + @param repeat The number of times to execute the callback. 1=once, 0=forever + """ super(Timer,self).__init__(delay) self._is_registered = False self._callback = callback @@ -129,6 +171,9 @@ self._executed = 0 def start(self): + """ + Must be called before the timer will start + """ if self._is_registered: return self._is_registered = True @@ -136,12 +181,18 @@ self._manager.registerEvent(self) def stop(self): + """ + Stops the timer + """ if not self._is_registered: return self._is_registered = False self._manager.unregisterEvent(self) def updateEvent(self,delta): + """ + Should not be called directly. This is called by FIFE. + """ if callable(self._callback): self._callback() @@ -150,4 +201,4 @@ if self._executed >= self._repeat: self.stop() - \ No newline at end of file +
--- a/demos/shooter/scripts/gui/guis.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/gui/guis.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or @@ -139,6 +139,9 @@ self._score = int(score) class HighScores(object): + """ + Handles all the high scores. It saves and loads the high score file. + """ def __init__(self, world): self._world = world self.load() @@ -375,4 +378,4 @@ self._widget.show() def hide(self): - self._widget.hide() \ No newline at end of file + self._widget.hide()
--- a/demos/shooter/scripts/powerups.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/powerups.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or @@ -85,4 +85,4 @@ self._scene.soundmanager.playClip(self._pickupclip) self.destroy() self._scene.queueObjectForRemoval(self) - \ No newline at end of file +
--- a/demos/shooter/scripts/scene.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/scene.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or @@ -29,7 +29,17 @@ from scripts.common.helpers import Rect class SceneNode(object): + """ + SceneNode + + This represents a node in the scene. The node stores a list + of objects that exist in the node. This is used by the Scene + to reduce the number of collision checks. + """ def __init__(self, spaceobjects = None): + """ + @param spaceobjects A list of spaceobjects that will exist in this node + """ if not spaceobjects: self._spaceobjects = list() else: @@ -44,7 +54,19 @@ spaceobjects = property(_getObjects, _setObjects) class Scene(object): + """ + Scene + + This is the meat and potatoes of the game. This class takes care of the scene graph, + updating objects, destroying objects, collision detection, etc etc. + """ def __init__(self, world, engine, objectLayer, soundmanager): + """ + @param world A reference to the master instance of the World class + @param engine A reference to the FIFE engine + @param objectLayer The layer that all objects exist on + @param soundmanager A reference to the SoundManager + """ self._engine = engine self._world = world self._model = engine.getModel() @@ -73,6 +95,9 @@ self._music = None def destroyScene(self): + """ + Removes all objects from the scene and deletes them from the layer. + """ nodestodelete = list() objtodelete = list() @@ -95,6 +120,15 @@ self._nodes.remove(node) def initScene(self, mapobj): + """ + Initializess the scene and scene graph. This creates game objects for + FIFE instances that exist in the map. + + The scene graph is a one dimensional graph. Each node represents one layer + unit. Objects are added to nodes in the scene graph based on their exact + layer coordinates. + """ + #initialize our scene array to some arbitrary size for i in range(0,self._maxnodes): self._nodes.append(SceneNode()) @@ -183,6 +217,9 @@ self.startCamera() def musicHasFinished(self): + """ + Sound callback example that gets fired after the music has finished playing. + """ print self._music.name + " has finished playing. Starting it again...\n" def pause(self, time): @@ -206,12 +243,26 @@ self._world.endLevel() def queueObjectForRemoval(self, obj): + """ + You should use this function to remove an object from the scene. + The garbage collector is what ultimately deletes the object from memory. + DO NOT do this yourself as you may cause issues with segfaults. + """ self._objectstodelete.append(obj) def getObjectsInNode(self, nodeindex): + """ + @param nodeindex the index of the node you which to retrieve objects from. + @return The list of objects in the specified node index + """ return self._nodes[nodeindex].instances def getObjectsInRange(self, rangeL, rangeR): + """ + @param rangeL the left most node index + @param rangeR the right most node index + @return A combined list of objects in the specified node index range (inclusive) + """ objects = list() for i in range(rangeL, rangeR): @@ -220,6 +271,12 @@ return objects def addObjectToScene(self, obj): + """ + Adds an object to the scene in the correct scene node + + @param obj The object to add to the scene + """ + #TODO: search to ensure the object isn't already part of the scene loc = obj.instance.getLocation().getExactLayerCoordinates() nodeindex = int(loc.x * self._xscale) @@ -232,6 +289,13 @@ self.queueObjectForRemoval(obj) def moveObjectInScene(self, obj): + """ + When an object moves in the scene you should call this function to update + scene graph. You MUST do this or the graph will be incorrect. + + @param obj The object to move in the scene + """ + loc = obj.instance.getLocation().getExactLayerCoordinates() nodeindex = int(loc.x * self._xscale) @@ -247,6 +311,15 @@ self.queueObjectForRemoval(obj) def removeObjectFromScene(self, obj): + """ + You would not normally call this function directly. You should probably + call queueObjectForRemoval(). + + This function releases any memory allocated for the object by deleting + the FIFE instance. + + @param obj The object to delete + """ for node in self._nodes: if obj in node.spaceobjects: if obj.instance: @@ -263,15 +336,25 @@ self._cameraspeed = 0 def startCamera(self): + """ + Starts the camera moving slowly to the right. + """ self._cameraspeed = 0.001 def collectGarbage(self): + """ + This should be called once a frame. It removes the object from the scene. + """ for obj in self._objectstodelete: self.removeObjectFromScene(obj) self._objectstodelete = list() def update(self, time, keystate): + """ + This function should be called once a frame. This is where all the game logic + happens. + """ timedelta = (time - self._timemod) - self._time self._timedelta = timedelta self._time = time - self._timemod
--- a/demos/shooter/scripts/ships/enemies.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/ships/enemies.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or @@ -239,4 +239,4 @@ elif self.hitpoints == 10: self.weapon = FireBallSpread(self._scene, self, 2000) self.weapon.lastfired = self._scene.time - \ No newline at end of file +
--- a/demos/shooter/scripts/ships/player.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/ships/player.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or @@ -220,4 +220,4 @@ score = property(_getScore) lives = property(_getLives, _setLives) - invulnerable = property(_getInvulnerable, _setInvulnerable) \ No newline at end of file + invulnerable = property(_getInvulnerable, _setInvulnerable)
--- a/demos/shooter/scripts/ships/shipbase.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/ships/shipbase.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or @@ -29,7 +29,19 @@ class ShipActionListener(fife.InstanceActionListener): + """ + This class is an action listener that listens for instance actions + that are complete. It is used in this demo to specify what should happen + after the object has completed flashing or exploding. + + There should be an instance of this listener for every ship that you want + to be removed from the scene after exploding. + """ def __init__(self, ship): + """ + @param ship The ship that this actionlistener belongs to. + """ + fife.InstanceActionListener.__init__(self) self._ship = ship @@ -48,7 +60,22 @@ self._ship.removeFromScene() class Ship(SpaceObject): + """ + Ship + + This is the base ship object. The Player and Enemy ship classes inherit + this class. This class extends SpaceObject by allowing Weapons to + be attached to the object as well as hit points. + """ def __init__(self, scene, name, findInstance=True): + """ + @param scene A reference to the Scene + @param name The name of the ship + @param findInstance True if the instance you are looking for is already loaded + False if you want to load the instance yourself + + """ + super(Ship, self).__init__(scene, name, findInstance) self._weapon = None @@ -69,18 +96,33 @@ return self._weapon def flash(self, number): + """ + Playes the flash animation (or action) the specified number of times + + @param number an integer specifying the number of times to play the flash animation + """ if self._running: self._instance.act('flash', self._instance.getFacingLocation()) self._flashnumber = number self._flashing = True def fire(self, direction): + """ + Fires the current weapon in the specified direction + + @param direction A fife.DoublePoint() specifying the direction to fire + """ if self._weapon and self._hitpoints > 0: return self._weapon.fire(direction) return None def applyHit(self, hp): + """ + Removes the specified number of hit points. Destroys the ship if necessary. + + @param hp The number of hit points to remove from the ship. + """ self._hitpoints -= hp if self._hitpoints <= 0: self.destroy() @@ -88,6 +130,9 @@ self._scene.soundmanager.playClip(self._hitclip) def destroy(self): + """ + Plays the explode animation (or action) + """ if self._running: self._instance.act('explode', self._instance.getFacingLocation()) self._scene.soundmanager.playClip(self._explodclip) @@ -107,4 +152,4 @@ weapon = property(_getWeapon, _setWeapon) hitpoints = property(_getHitPoints, _setHitPoints) - scorevalue = property(_getScoreValue, _setScoreValue) \ No newline at end of file + scorevalue = property(_getScoreValue, _setScoreValue)
--- a/demos/shooter/scripts/soundmanager.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/soundmanager.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or @@ -35,15 +35,33 @@ looping). All instances of SoundClip should be created by SoundManager. """ def __init__(self, soundmanager, clipid, soundname, emitter): + """ + @param soundmanager A reference to the SoundManager + @param clipid The FIFE sound clip ID from the sound clip pool + @param soundname The filename of the sound + @param emitter A reference to the FIFE SoundEmitter associated with this clip + + """ self._soundmanager = soundmanager self._name = soundname - self._fifeclipid = clipid + + #The FIFE SoundEmitter associated with this SoundClip. + #Note that we do NOT own the emitter. self._fifeemitter = emitter self._fifeemitter.thisown = 0 + self._fifeclipid = clipid + + #0 = mute, 255 = normal volume self._gain = 255.0 self._looping = False + + #if you set the callback it will be executed after the sound + #has finished playing. self._callback = None + + #length of the sound self._duration = 0 + self._timer = None def _getClipID(self): @@ -108,16 +126,24 @@ FIFE SoundEmitter per unique sound. """ def __init__(self, engine): + """ + @param engine A reference to the FIFE engine + """ self._engine = engine self._fifesoundmanager = self._engine.getSoundManager() self._fifesoundmanager.init() + #A dict of fife emitters self._loadedclips = {} def loadSoundClip(self, filename): """ Returns a valid SoundClip instance. + + @param filename the relative path and filename of the sound file + + @return Returns a new SoundClip instance. """ if not self._loadedclips.has_key(filename): clipid = self._engine.getSoundClipPool().addResourceFromFile(filename) @@ -136,6 +162,24 @@ return clip def playClip(self, clip): + """ + Plays a sound clip. + + This function does not use the FIFE + emitters "looping" property to loop a sound. Instead + it registers a new timer and uses the duration of the + clip as the timer length. + + If the SoundClip is invalid (no fifeemitter) then + it attempts to load it before playing it. + + Note that this will stop any clips that use the same + FIFE emitter. You cannot play the same sound more than + once at a time. + + @param clip The SoundClip to be played + """ + if clip.fifeemitter: if clip.callback: if clip.timer: @@ -165,6 +209,12 @@ self.playClip(clip) def stopClip(self, clip): + """ + Stops playing the sound clip. Note that this will + affect all clips that use the same FIFE emitter. + + @parm clip The SoundClip to stop. + """ if clip.fifeemitter: clip.fifeemitter.stop() @@ -177,6 +227,10 @@ emitter.stop() def destroy(self): + """ + Releases all FIFE emitters. This does not free the resources + from the FIFE sound clip pool. + """ self.stopAllSounds() for emitter in self._loadedclips.values():
--- a/demos/shooter/scripts/weapons.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/weapons.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or
--- a/demos/shooter/scripts/world.py Wed May 05 21:39:31 2010 +0000 +++ b/demos/shooter/scripts/world.py Thu May 06 19:34:21 2010 +0000 @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # #################################################################### -# Copyright (C) 2005-2009 by the FIFE team -# http://www.fifengine.de +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or