View Javadoc

1   /*
2    * Copyright (C) 2005 Erland Isaksson (erland_i@hotmail.com)
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   */
19  package erland.game.tileadventure;
20  
21  public class MoveAction extends Action {
22      private Vector3D direction;
23  
24      public static final Vector3D WEST = new Vector3D(-0.04f,0,0);
25      public static final Vector3D EAST = new Vector3D(0.04f,0,0);
26      public static final Vector3D NORTH = new Vector3D(0,-0.04f,0);
27      public static final Vector3D SOUTH = new Vector3D(0,0.04f,0);
28      public static final Vector3D UP = new Vector3D(0,0,0.04f);
29      public static final Vector3D DOWN = new Vector3D(0,0,-0.04f);
30  
31      public static final Vector3D NONE = new Vector3D(0,0,0);
32  
33      public MoveAction(ControllerInterface controller, Vector3D direction) {
34          this.direction = direction;
35          setController(controller);
36      }
37  
38      public void start() {
39          super.start();
40          getController().getControlledObject().setMoveVector(direction);
41      }
42  
43      public void stop() {
44          super.stop();
45          getController().getControlledObject().setMoveVector(NONE);
46      }
47  
48      public boolean perform() {
49          GameObject o = getController().getControlledObject();
50          if(o.getMoveVector().getLength()>0) {
51              float x = o.getPosX()+direction.getX();
52              float y = o.getPosY()+direction.getY();
53              float z = o.getPosZ()+direction.getZ();
54              if(o.getActionMap().move(o,x,y,z)) {
55                  return false;
56              }else {
57                  return true;
58              }
59          }
60          return true;
61      }
62  }