# HG changeset patch
# User prock@33b003aa-7bff-0310-803a-e67f0ece8222
# Date 1270050000 0
# Node ID 2046a1f2f5f25ed50426bc6ed292c0e051a96c52
# Parent f855809822cf4142d341352ffd5d0561766a1ee8
Adding the shooter demo. This is still a work in progress.
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/fonts/FreeSans.ttf
Binary file demos/shooter/fonts/FreeSans.ttf has changed
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/gui/mainwindow.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/gui/mainwindow.xml Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/maps/shooter_map1.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/maps/shooter_map1.xml Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,457 @@
+
+
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/objects/backgrounds/starfield/object.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/objects/backgrounds/starfield/object.xml Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,4 @@
+
+
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/objects/backgrounds/starfield/starfield.png
Binary file demos/shooter/objects/backgrounds/starfield/starfield.png has changed
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/objects/projectiles/bullet1/bullet1.png
Binary file demos/shooter/objects/projectiles/bullet1/bullet1.png has changed
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/objects/projectiles/bullet1/object.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/objects/projectiles/bullet1/object.xml Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,4 @@
+
+
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/objects/ships/player/object.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/objects/ships/player/object.xml Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,4 @@
+
+
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/objects/ships/player/ship1.png
Binary file demos/shooter/objects/ships/player/ship1.png has changed
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/run.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/run.py Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,125 @@
+#!/usr/bin/env python
+
+# -*- coding: utf-8 -*-
+
+# ####################################################################
+# Copyright (C) 2005-2009 by the FIFE team
+# http://www.fifengine.de
+# This file is part of FIFE.
+#
+# FIFE is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+# ####################################################################
+# This is the rio de hola client for FIFE.
+
+import sys, os, re, math, random, shutil
+
+fife_path = os.path.join('..','..','engine','python')
+if os.path.isdir(fife_path) and fife_path not in sys.path:
+ sys.path.insert(0,fife_path)
+
+from fife import fife
+print "Using the FIFE python module found here: ", os.path.dirname(fife.__file__)
+
+from fife.extensions import *
+from scripts import world
+from scripts.common import eventlistenerbase
+from fife.extensions.basicapplication import ApplicationBase
+from fife.extensions import pychan
+from fife.extensions.pychan import widgets
+
+class ApplicationListener(eventlistenerbase.EventListenerBase):
+ def __init__(self, engine, world):
+ super(ApplicationListener, self).__init__(engine,regKeys=True,regCmd=True, regMouse=False, regConsole=True, regWidget=True)
+ self.engine = engine
+ self.world = world
+ engine.getEventManager().setNonConsumableKeys([
+ fife.Key.ESCAPE,])
+
+ self.quit = False
+
+ def keyPressed(self, evt):
+ keyval = evt.getKey().getValue()
+ keystr = evt.getKey().getAsString().lower()
+ consumed = False
+ if keyval == fife.Key.ESCAPE:
+ self.quit = True
+ evt.consume()
+
+ def onCommand(self, command):
+ self.quit = (command.getCommandType() == fife.CMD_QUIT_GAME)
+ if self.quit:
+ command.consume()
+
+class Tutorial(ApplicationBase):
+ def __init__(self):
+ super(Tutorial,self).__init__()
+ pychan.init(self.engine, debug=False)
+ self.world = world.World(self.engine)
+ self.listener = ApplicationListener(self.engine, self.world)
+ self.world.load("maps/shooter_map1.xml")
+
+ def loadSettings(self):
+ """
+ Load the settings from a python file and load them into the engine.
+ Called in the ApplicationBase constructor.
+ """
+
+ engineSetting = self.engine.getSettings()
+ engineSetting.setDefaultFontGlyphs(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\"")
+ engineSetting.setDefaultFontPath("fonts/FreeSans.ttf")
+ engineSetting.setDefaultFontSize(12)
+ engineSetting.setBitsPerPixel(16)
+ engineSetting.setScreenWidth(1024)
+ engineSetting.setScreenHeight(768)
+ engineSetting.setRenderBackend("OpenGL")
+ engineSetting.setFullScreen(0)
+
+ try:
+ engineSetting.setWindowTitle("FIFE - Shooter")
+ except:
+ pass
+ try:
+ engineSetting.setImageChunkingSize(256)
+ except:
+ pass
+
+ def initLogging(self):
+ """
+ Initialize the LogManager.
+ """
+ #LogModules = list()
+ #LogModules.append("controller")
+ LogModules = ["controller",]
+ self.log = fifelog.LogManager(self.engine, 1, 0)
+ if LogModules:
+ self.log.setVisibleModules(*LogModules)
+
+ def createListener(self):
+ pass # already created in constructor
+
+ def _pump(self):
+ if self.listener.quit:
+ self.breakRequested = True
+ else:
+ self.world.pump()
+
+def main():
+ app = Tutorial()
+ app.run()
+
+
+if __name__ == '__main__':
+ main()
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/scripts/__init__.py
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/scripts/common/__init__.py
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/scripts/common/eventlistenerbase.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/scripts/common/eventlistenerbase.py Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,73 @@
+# -*- coding: utf-8 -*-
+
+# ####################################################################
+# Copyright (C) 2005-2009 by the FIFE team
+# http://www.fifengine.de
+# This file is part of FIFE.
+#
+# FIFE is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+# ####################################################################
+
+from fife import fife
+
+class EventListenerBase(fife.IKeyListener, fife.ICommandListener, fife.IMouseListener, fife.ConsoleExecuter):
+ def __init__(self, engine, regKeys=False, regCmd=False, regMouse=False, regConsole=False, regWidget=False):
+ self.eventmanager = engine.getEventManager()
+
+ fife.IKeyListener.__init__(self)
+ if regKeys:
+ self.eventmanager.addKeyListener(self)
+ fife.ICommandListener.__init__(self)
+ if regCmd:
+ self.eventmanager.addCommandListener(self)
+ fife.IMouseListener.__init__(self)
+ if regMouse:
+ self.eventmanager.addMouseListener(self)
+ fife.ConsoleExecuter.__init__(self)
+ if regConsole:
+ engine.getGuiManager().getConsole().setConsoleExecuter(self)
+
+
+ def mousePressed(self, evt):
+ pass
+ def mouseReleased(self, evt):
+ pass
+ def mouseEntered(self, evt):
+ pass
+ def mouseExited(self, evt):
+ pass
+ def mouseClicked(self, evt):
+ pass
+ def mouseWheelMovedUp(self, evt):
+ pass
+ def mouseWheelMovedDown(self, evt):
+ pass
+ def mouseMoved(self, evt):
+ pass
+ def mouseDragged(self, evt):
+ pass
+ def keyPressed(self, evt):
+ pass
+ def keyReleased(self, evt):
+ pass
+ def onCommand(self, command):
+ pass
+ def onToolsClick(self):
+ print "No tools set up yet"
+ def onConsoleCommand(self, command):
+ pass
+ def onWidgetAction(self, evt):
+ pass
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/scripts/common/helpers.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/scripts/common/helpers.py Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,102 @@
+# -*- coding: utf-8 -*-
+
+# ####################################################################
+# Copyright (C) 2005-2009 by the FIFE team
+# http://www.fifengine.de
+# This file is part of FIFE.
+#
+# FIFE is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+# ####################################################################
+
+from fife import fife
+
+
+def normalize(vector):
+ """
+ Helper function to normalize a vector
+ """
+ norm = fife.DoublePoint(0,0)
+
+ invLength = 1.0/vector.length()
+ if invLength > 1e-06:
+ norm.x = vector.x * invLength;
+ norm.y = vector.y * invLength;
+ else:
+ norm.x = 0
+ norm.y = 0
+
+ return norm
+
+class Rect(object):
+ def __init__(self, x = 0, y = 0, w = 0, h = 0):
+ self._x = x
+ self._y = y
+ self._w = w
+ self._h = h
+
+ def intersects(self, rect):
+ _x = self._x - rect.x;
+ _y = self._y - rect.y;
+ _w = self._w;
+ _h = self._h;
+
+ if _x < 0:
+ _w += _x
+ _x = 0
+
+ if _y < 0:
+ _h += _y
+ _y = 0
+
+ if _x + _w > rect.w:
+ _w = rect.w - _x
+
+ if _y + _h > rect.h:
+ _h = rect.h - _y
+
+ if _w <= 0 or _h <= 0:
+ return False
+
+ return True
+
+ def _setX(self, x):
+ self._x = x
+
+ def _getX(self):
+ return self._x
+
+ def _setY(self, y):
+ self._y = y
+
+ def _getY(self):
+ return self._y
+
+ def _setW(self, w):
+ self._w = w
+
+ def _getW(self):
+ return self._w
+
+ def _setH(self, h):
+ self._h = h
+
+ def _getH(self):
+ return self._h
+
+ x = property(_getX, _setX)
+ y = property(_getY, _setY)
+ w = property(_getW, _setW)
+ h = property(_getH, _setH)
\ No newline at end of file
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/scripts/scene.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/scripts/scene.py Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,75 @@
+# -*- coding: utf-8 -*-
+
+# ####################################################################
+# Copyright (C) 2005-2009 by the FIFE team
+# http://www.fifengine.de
+# This file is part of FIFE.
+#
+# FIFE is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+# ####################################################################
+
+from fife import fife
+from scripts.ships.shipbase import Ship
+from scripts.ships.player import Player
+from scripts.common.helpers import Rect
+
+class Scene(object):
+ def __init__(self, engine, objectLayer):
+ self._engine = engine
+ self._model = engine.getModel()
+ self._layer = objectLayer
+
+ self._player = Player(self._model, 'player', self._layer)
+ self._projectiles = list()
+ self._lasttime = 0
+
+ def attachCamera(self, cam):
+ self._camera = cam
+ self._camera.setLocation(self._player.location)
+
+ def _getPlayer(self):
+ return self._player
+
+ def update(self, time, keystate):
+ timedelta = time - self._lasttime
+ self._lasttime = time
+
+ self._player.update(timedelta, keystate, self._camera)
+
+ #update camera location
+ loc = self._player.location
+ exactloc = self._camera.getLocation().getExactLayerCoordinates()
+ exactloc.x += timedelta * 0.0005
+ loc.setExactLayerCoordinates(exactloc)
+ self._camera.setLocation(loc)
+
+ #update the list of projectiles
+ todelete = list()
+ for p in self._projectiles:
+ p.update(time)
+ if not p.running:
+ todelete.append(p)
+ for p in todelete:
+ self._projectiles.remove(p)
+
+ #fire the currently selected gun
+ if keystate['SPACE']:
+ prjct = self._player.fire(time)
+ if prjct:
+ self._projectiles.append(prjct)
+
+ player = property(_getPlayer)
+
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/scripts/ships/__init__.py
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/scripts/ships/player.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/scripts/ships/player.py Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,87 @@
+# -*- coding: utf-8 -*-
+
+# ####################################################################
+# Copyright (C) 2005-2009 by the FIFE team
+# http://www.fifengine.de
+# This file is part of FIFE.
+#
+# FIFE is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+# ####################################################################
+
+from fife import fife
+from scripts.ships.shipbase import Ship
+from scripts.common.helpers import Rect
+
+
+class Player(Ship):
+ def __init__(self, scene, shipName, layer):
+ super(Player, self).__init__(scene, shipName, layer, True)
+ self._bounds = Rect(-100,-100,200,200)
+
+ def update(self, timedelta, keystate, camera):
+ key = False
+
+ oldpos = self.location
+
+ if keystate['UP']:
+ self.applyThrust(fife.DoublePoint(0,-0.075))
+ key = True
+ if keystate['DOWN']:
+ self.applyThrust(fife.DoublePoint(0,0.075))
+ key = True
+ if keystate['LEFT']:
+ self.applyThrust(fife.DoublePoint(-0.075,0))
+ key = True
+ if keystate['RIGHT']:
+ self.applyThrust(fife.DoublePoint(0.075,0))
+ key = True
+
+ if not key and self._velocity.length() > 0:
+ self.applyBrake(0.075)
+
+ super(Player, self).update(timedelta)
+
+ #set up the players camera bounds
+ #TODO: grab screen resolution from somewhere
+ topleft = camera.toMapCoordinates(fife.ScreenPoint(0,0))
+ bottomright = camera.toMapCoordinates(fife.ScreenPoint(1024,768))
+
+ #add a little padding to the left edge
+ topleft.x += 0.1
+
+ camrect = Rect(topleft.x, topleft.y, bottomright.x - topleft.x, bottomright.y - topleft.y)
+
+ #player bounding box
+ #TODO: make this configurable
+ pos = self.location.getExactLayerCoordinates()
+ bbox = Rect()
+ bbox.x = pos.x - 0.005
+ bbox.y = pos.y - 0.005
+ bbox.w = 0.01
+ bbox.h = 0.01
+
+ if not bbox.intersects(camrect):
+ if pos.x < topleft.x:
+ pos.x += timedelta * 0.0005
+ oldpos.setExactLayerCoordinates(pos)
+ self._velocity.x = timedelta * 0.0005
+ else:
+ self._velocity.x = 0
+
+ self._velocity.y = 0
+
+ self.location = oldpos
+
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/scripts/ships/shipbase.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/scripts/ships/shipbase.py Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,136 @@
+# -*- coding: utf-8 -*-
+
+# ####################################################################
+# Copyright (C) 2005-2009 by the FIFE team
+# http://www.fifengine.de
+# This file is part of FIFE.
+#
+# FIFE is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+# ####################################################################
+
+from fife import fife
+from scripts.common.helpers import normalize
+from scripts.weapons import Weapon
+
+class Ship(object):
+ def __init__(self, scene, shipName, layer, uniqInMap=True):
+ self._scene = scene
+ self._name = shipName
+ self._layer = layer
+ if uniqInMap:
+ self._instance = self._layer.getInstance(self._name)
+ else:
+ #have to create instance here
+ self._instance = None
+
+ #velocity as a vector
+ self._velocity = fife.DoublePoint(0,0)
+ self._maxvelocity = 0.025
+ self._timedelta = 0
+ self._weapon = None
+
+ def _getMaxVelocity(self):
+ return self._maxvelocity
+
+ def _setMaxVelocity(self, maxvel):
+ self._maxvelocity = maxvel
+
+ def _getLocation(self):
+ return self._instance.getLocation()
+
+ def _setLocation(self, loc):
+ self._instance.setLocation(loc)
+
+ def _getInstance(self):
+ return self._instance
+
+ def _getVelocity(self):
+ return self._velocity
+
+ def _setWeapon(self, weapon):
+ self._weapon = weapon
+
+ def _getWeapon(self, weapon):
+ return self._weapon
+
+ def applyThrust(self, vector):
+ self._velocity.x += vector.x * (self._timedelta/1000.0)
+ self._velocity.y += vector.y * (self._timedelta/1000.0)
+
+ if self._velocity.length() > self._maxvelocity:
+ norm = normalize(self._velocity)
+ self._velocity.x = norm.x * self._maxvelocity
+ self._velocity.y = norm.y * self._maxvelocity
+
+
+ def applyBrake(self, brakingForce):
+
+ if self._velocity.length() <= .001:
+ self._velocity.x = 0
+ self._velocity.y = 0
+ return
+
+ #first normalize to get a unit vector of the direction we are traveling
+ norm = normalize(self._velocity)
+ if norm.length() == 0:
+ self._velocity.x = 0
+ self._velocity.y = 0
+ return
+
+ #negate to get opposite direction
+ norm.x = norm.x * -1
+ norm.y = norm.y * -1
+
+ #apply braking deceleration
+ norm.x *= brakingForce
+ norm.y *= brakingForce
+
+ self._velocity.x += norm.x * (self._timedelta/1000.0)
+ self._velocity.y += norm.y * (self._timedelta/1000.0)
+
+ def fire(self, curtime):
+ if self._weapon:
+ return self._weapon.fire(curtime)
+
+ return None
+
+ def start(self):
+ pass
+
+ def update(self, timedelta):
+ self._timedelta = timedelta
+
+ shiploc = self.location
+ exactloc = shiploc.getExactLayerCoordinates()
+
+ exactloc.x += self._velocity.x
+ exactloc.y += self._velocity.y
+
+ shiploc.setExactLayerCoordinates(exactloc)
+ self.location = shiploc
+
+ def stop(self):
+ pass
+
+ def destroy(self):
+ pass
+
+ location = property(_getLocation,_setLocation)
+ instance = property(_getInstance)
+ velocity = property(_getVelocity)
+ maxvelocity = property(_getMaxVelocity, _setMaxVelocity)
+ weapon = property(_getWeapon, _setWeapon)
+
\ No newline at end of file
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/scripts/weapons.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/scripts/weapons.py Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,101 @@
+# -*- coding: utf-8 -*-
+
+# ####################################################################
+# Copyright (C) 2005-2009 by the FIFE team
+# http://www.fifengine.de
+# This file is part of FIFE.
+#
+# FIFE is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+# ####################################################################
+
+from fife import fife
+
+class Projectile(object):
+ def __init__(self, model, projectileName, layer, timeToLive):
+ self._model = model
+ self._name = projectileName
+ self._layer = layer
+ self._instance = None
+ self._velocity = None
+ self._obj = self._model.getObject(self._name, "http://www.fifengine.de/xml/tutorial")
+ self._running = False
+ self._ttl = timeToLive
+ self._starttime = 0
+
+ def create(self, location):
+ self._instance = self._layer.createInstance(self._obj, location.getExactLayerCoordinates(), "bullet")
+ fife.InstanceVisual.create(self._instance)
+
+ def run(self, velocity, location, time):
+ if not self._running:
+ self._velocity = velocity
+
+ self.create(location)
+ self._running = True
+
+ self._starttime = time
+
+ def destroy(self):
+ if self._running and self._instance:
+ self._layer.deleteInstance(self._instance)
+ self._running = False
+
+ def _getLocation(self):
+ return self._instance.getLocation()
+
+ def _setLocation(self, loc):
+ self._instance.setLocation(loc)
+
+ def _isRunning(self):
+ return self._running
+
+ def _getTTL(self):
+ return self._ttl
+
+ def update(self, curtime):
+ if self._running and (curtime - self._starttime) < self._ttl:
+ projloc = self.location
+ exactloc = projloc.getExactLayerCoordinates()
+
+ exactloc.x += self._velocity.x
+ exactloc.y += self._velocity.y
+
+ projloc.setExactLayerCoordinates(exactloc)
+ self.location = projloc
+ else:
+ self.destroy()
+
+ location = property(_getLocation,_setLocation)
+ running = property(_isRunning)
+ ttl = property(_getTTL)
+
+class Weapon(object):
+ def __init__(self, model, layer, ship, firerate):
+ self._model = model
+ self._layer = layer
+ self._ship = ship
+ self._firerate = firerate
+ self._lastfired = 0
+
+ def fire(self, curtime):
+ if (curtime - self._lastfired) > self._firerate:
+ pjctl = Projectile(self._model, "bullet1", self._layer, 2000 )
+ pjctl.run(fife.DoublePoint(0.075,0), self._ship.location, curtime)
+ self._lastfired = curtime
+ return pjctl
+
+ return None
+
diff -r f855809822cf -r 2046a1f2f5f2 demos/shooter/scripts/world.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/scripts/world.py Wed Mar 31 15:40:00 2010 +0000
@@ -0,0 +1,180 @@
+# -*- coding: utf-8 -*-
+
+# ####################################################################
+# Copyright (C) 2005-2009 by the FIFE team
+# http://www.fifengine.de
+# This file is part of FIFE.
+#
+# FIFE is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+# ####################################################################
+
+from fife import fife
+import math, random
+from fife.extensions import pychan
+from fife.extensions.pychan import widgets
+
+from scripts.common.eventlistenerbase import EventListenerBase
+from scripts.common.helpers import normalize
+from fife.extensions.loaders import loadMapFile
+
+from scripts.ships.shipbase import Ship
+from scripts.ships.player import Player
+from scripts.scene import Scene
+from scripts.weapons import Weapon
+
+class World(EventListenerBase):
+ """
+ The world!
+
+ This class handles:
+ setup of map view (cameras ...)
+ loading the map
+ GUI for right clicks
+ handles mouse/key events which aren't handled by the GUI.
+ ( by inheriting from EventlistenerBase )
+
+ That's obviously too much, and should get factored out.
+ """
+ def __init__(self, engine):
+ super(World, self).__init__(engine, regMouse=True, regKeys=True)
+ self.engine = engine
+ self.timemanager = engine.getTimeManager()
+ self.eventmanager = engine.getEventManager()
+ self.model = engine.getModel()
+ self.filename = ''
+ self.keystate = { 'UP': False, 'DOWN': False, 'LEFT': False, 'RIGHT': False, 'CTRL': False, 'SPACE': False, }
+ self.dynamic_widgets = {}
+ self.pump_ctr = 0
+ self.map = None
+ self.scene = None
+
+ def reset(self):
+ """
+ Clear the agent information and reset the moving secondary camera state.
+ """
+ self.cameras = {}
+
+ def load(self, filename):
+ """
+ Load a xml map and setup agents and cameras.
+ """
+ self.filename = filename
+ self.reset()
+ self.map = loadMapFile(filename, self.engine)
+
+ self.scene = Scene(self.engine, self.map.getLayer('objects'))
+ #self.initPlayer()
+ self.initCameras()
+
+ self.mainwindow = pychan.loadXML('gui/mainwindow.xml')
+ self.fpstext = self.mainwindow.findChild(name="fps")
+ self.velocitytext = self.mainwindow.findChild(name="velocity")
+ self.positiontext = self.mainwindow.findChild(name="position")
+ self.mainwindow.position = (0,0)
+ self.mainwindow.show()
+
+ #give player the default weapon
+ self.scene.player.weapon = Weapon(self.model, self.map.getLayer('objects'), self.scene.player, 100)
+
+ def initCameras(self):
+ """
+ Before we can actually see something on screen we have to specify the render setup.
+ This is done through Camera objects which offer a viewport on the map.
+ """
+
+ for cam in self.map.getCameras():
+ if cam.getId() == 'main':
+ self.cameras['main'] = cam
+
+ self.scene.attachCamera(self.cameras['main'])
+
+
+ def keyPressed(self, evt):
+ keyval = evt.getKey().getValue()
+ keystr = evt.getKey().getAsString().lower()
+ if keyval == fife.Key.UP:
+ self.keystate['UP'] = True
+ elif keyval == fife.Key.DOWN:
+ self.keystate['DOWN'] = True
+ elif keyval == fife.Key.LEFT:
+ self.keystate['LEFT'] = True
+ elif keyval == fife.Key.RIGHT:
+ self.keystate['RIGHT'] = True
+ elif keyval == fife.Key.SPACE:
+ self.keystate['SPACE'] = True
+ elif keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
+ self.keystate['CTRL'] = True
+
+ def keyReleased(self, evt):
+ keyval = evt.getKey().getValue()
+ if keyval == fife.Key.UP:
+ self.keystate['UP'] = False
+ elif keyval == fife.Key.DOWN:
+ self.keystate['DOWN'] = False
+ elif keyval == fife.Key.LEFT:
+ self.keystate['LEFT'] = False
+ elif keyval == fife.Key.RIGHT:
+ self.keystate['RIGHT'] = False
+ elif keyval == fife.Key.SPACE:
+ self.keystate['SPACE'] = False
+ elif keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
+ self.keystate['CTRL'] = False
+
+ def mouseWheelMovedUp(self, evt):
+ if self.keystate['CTRL']:
+ self.cameras['main'].setZoom(self.cameras['main'].getZoom() * 1.05)
+
+ def mouseWheelMovedDown(self, evt):
+ if self.keystate['CTRL']:
+ self.cameras['main'].setZoom(self.cameras['main'].getZoom() / 1.05)
+
+ def mousePressed(self, evt):
+ if evt.isConsumedByWidgets():
+ return
+
+ clickpoint = fife.ScreenPoint(evt.getX(), evt.getY())
+
+ def mouseMoved(self, evt):
+ pt = fife.ScreenPoint(evt.getX(), evt.getY())
+
+ def pump(self):
+ """
+ Called every frame.
+ """
+
+ #update the scene
+ self.scene.update(self.timemanager.getTime(), self.keystate)
+
+
+ #update the HUD
+ avgframe = self.timemanager.getAverageFrameTime()
+ if avgframe > 0:
+ fps = 1 / (avgframe / 1000)
+ else:
+ fps = 0
+ fpstxt = "%3.2f" % fps
+ self.fpstext.text = unicode(fpstxt)
+
+ player = self.scene.player
+ exactcoords = player.location.getExactLayerCoordinates()
+ pos = "%1.2f" % exactcoords.x + ", %1.2f" % exactcoords.y
+ self.positiontext.text = unicode(pos)
+
+ vel = "%1.2f" % player.velocity.x + ", %1.2f" % player.velocity.y
+ self.velocitytext.text = unicode(vel)
+
+
+ self.pump_ctr += 1