| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# #################################################################### |
| 5 |
# Copyright (C) 2005-2019 by the FIFE team |
| 6 |
# http://www.fifengine.net |
| 7 |
# This file is part of FIFE. |
| 8 |
# |
| 9 |
# FIFE is free software; you can redistribute it and/or |
| 10 |
# modify it under the terms of the GNU Lesser General Public |
| 11 |
# License as published by the Free Software Foundation; either |
| 12 |
# version 2.1 of the License, or (at your option) any later version. |
| 13 |
# |
| 14 |
# This library is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
# Lesser General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU Lesser General Public |
| 20 |
# License along with this library; if not, write to the |
| 21 |
# Free Software Foundation, Inc., |
| 22 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 |
# #################################################################### |
| 24 |
|
| 25 |
from .swig_test_utils import * |
| 26 |
from fife.extensions.serializers.xmlanimation import loadXMLAnimation |
| 27 |
|
| 28 |
class ActionTests(unittest.TestCase): |
| 29 |
def setUp(self): |
| 30 |
template = 'tests/data/wolf_walk/wolf_walk_%s.xml' |
| 31 |
dirnames = ['e', 'ne', 'n', 'nw', 'w', 'sw', 's', 'se'] |
| 32 |
files = [template % dirname for dirname in dirnames] |
| 33 |
|
| 34 |
self.engine = getEngine() |
| 35 |
|
| 36 |
self.map = self.engine.getModel().createMap("map001") |
| 37 |
self.grid = self.engine.getModel().getCellGrid("square") |
| 38 |
|
| 39 |
self.layer = self.map.createLayer("Layer001", self.grid) |
| 40 |
self.layer.setWalkable(True) |
| 41 |
self.layer.createCellCache() |
| 42 |
|
| 43 |
self.target = fife.Location(self.layer) |
| 44 |
|
| 45 |
self.obj = fife.Object("object001", 'plaa') |
| 46 |
fife.ObjectVisual.create(self.obj) |
| 47 |
self.pather = fife.RoutePather() |
| 48 |
|
| 49 |
self.obj.setPather(self.pather) |
| 50 |
self.action = self.obj.createAction('walk') |
| 51 |
fife.ActionVisual.create(self.action) |
| 52 |
|
| 53 |
for index, direction in enumerate(dirnames): |
| 54 |
degree = 45 * index |
| 55 |
self.action.get2dGfxVisual().addAnimation(degree, loadXMLAnimation(self.engine, files[index])) |
| 56 |
|
| 57 |
self.ground = fife.Object("ground", 'plaa') |
| 58 |
image = self.engine.getImageManager().load('tests/data/earth_1.png') |
| 59 |
fife.ObjectVisual.create(self.ground) |
| 60 |
self.ground.get2dGfxVisual().addStaticImage(0, image.getHandle()) |
| 61 |
self.ground.img = self.engine.getImageManager().get(image.getHandle()) |
| 62 |
|
| 63 |
for y in range(-2,3): |
| 64 |
for x in range(-2,3): |
| 65 |
inst = self.layer.createInstance(self.ground, fife.ModelCoordinate(x,y)) |
| 66 |
fife.InstanceVisual.create(inst) |
| 67 |
|
| 68 |
self.inst = self.layer.createInstance(self.obj, fife.ModelCoordinate(-2,-2)) |
| 69 |
fife.InstanceVisual.create(self.inst) |
| 70 |
|
| 71 |
def tearDown(self): |
| 72 |
self.engine.destroy() |
| 73 |
|
| 74 |
def _testWalkingAction(self): |
| 75 |
|
| 76 |
self.inst.move('walk', self.target, 0.05) |
| 77 |
self.engine.initializePumping() |
| 78 |
backend = self.engine.renderBackend |
| 79 |
for i in range(360): |
| 80 |
self.inst.getLocation().getLayerCoordinates() |
| 81 |
self.target.getLayerCoordinates() |
| 82 |
if self.inst.getLocation().getLayerCoordinates() == self.target.getLayerCoordinates(): |
| 83 |
break |
| 84 |
self.inst.update() |
| 85 |
action = self.inst.currentAction |
| 86 |
angle = 0 #self.inst.orientation |
| 87 |
animation = action.getAnimationByAngle(angle) |
| 88 |
timestamp = self.inst.actionRuntime % animation.duration |
| 89 |
image = animation.getFrameByTimestamp( timestamp ) |
| 90 |
if image: |
| 91 |
image.render(fife.Rect(0,0,image.width,image.height),255) |
| 92 |
self.engine.pump() |
| 93 |
self.engine.finalizePumping() |
| 94 |
|
| 95 |
def testWalkAround(self): |
| 96 |
|
| 97 |
rb = self.engine.getRenderBackend() |
| 98 |
viewport = fife.Rect(0, 0, rb.getWidth(), rb.getHeight()) |
| 99 |
|
| 100 |
cam = self.map.addCamera("foo", viewport) |
| 101 |
cam.setCellImageDimensions(self.ground.img.getWidth(), self.ground.img.getHeight()) |
| 102 |
cam.setRotation(45) |
| 103 |
cam.setTilt(40) |
| 104 |
|
| 105 |
renderer = fife.InstanceRenderer.getInstance(cam) |
| 106 |
renderer.activateAllLayers(self.map) |
| 107 |
|
| 108 |
self.engine.initializePumping() |
| 109 |
self.target.setLayerCoordinates(fife.ModelCoordinate(2,-2)) |
| 110 |
self.inst.move('walk', self.target, 0.9) |
| 111 |
targets = ( |
| 112 |
(2,0), (2,-1), (2,-2), (1,-2), |
| 113 |
(0,-2), (-1,-2), (-2,-2), (-2,-1), |
| 114 |
(-2,0), (-2,1), (-2,2), (-1,2), |
| 115 |
(0,2), (1,2), (2,2), (2,1)) |
| 116 |
for target in targets: |
| 117 |
l = self.inst.getLocation() |
| 118 |
l.setLayerCoordinates(fife.ModelCoordinate(0,0)) |
| 119 |
self.inst.setLocation(l) |
| 120 |
self.target.setLayerCoordinates(fife.ModelCoordinate(*target)) |
| 121 |
self.inst.move('walk', self.target, 0.9) |
| 122 |
for i in range(10): |
| 123 |
self.engine.pump() |
| 124 |
|
| 125 |
self.engine.finalizePumping() |
| 126 |
self.map.removeCamera("foo") |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
TEST_CLASSES = [ActionTests] |
| 131 |
|
| 132 |
if __name__ == '__main__': |
| 133 |
unittest.main() |