1 package erland.game.tileadventure; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 6 7 /* 8 * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com) 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 23 * 24 */ 25 26 public class MovableObject extends AnimatedObject implements GameObjectUpdateInterface { 27 /*** Logging instance */ 28 private static Log LOG = LogFactory.getLog(MovableObject.class); 29 private boolean bMoving; 30 private Action movingAction; 31 private int movingProgress = 0; 32 private final int MOVING_PROGRESS_MAX = 20; 33 private int movingSpeed=0; 34 private boolean bDropping; 35 private float droppingProgress = 0; 36 private int droppingHeight = 0; 37 private float droppingSpeed = 0; 38 private final int DROPPING_PROGRESS_MAX = 20; 39 /* 40 protected Action isActionPossible(Action action) { 41 if(!bMoving && (action.isMove() || action.isPush())) { 42 return getActionMap().isActionPossibleOnObject(this,action); 43 } 44 return Action.NONE; 45 } 46 protected boolean actionChild(Action action) { 47 if(!bMoving && (action.isMove() || action.isPush())) { 48 Action a = getActionMap().startActionOnObject(this,action); 49 if(a.isMove()||a.isPush()) { 50 bMoving = true; 51 movingAction = a; 52 movingProgress = 0; 53 if(a.isMove()) { 54 movingSpeed = 2; 55 }else { 56 movingSpeed = 1; 57 } 58 } 59 return true; 60 } 61 return false; 62 } 63 64 65 protected boolean isMovableChild(Direction direction) { 66 if(!bMoving) { 67 float newX=getPosX(); 68 float newY=getPosY(); 69 float newZ=getPosZ(); 70 if(direction==Direction.WEST) { 71 newX--; 72 }else if(direction==Direction.EAST) { 73 newX++; 74 }else if(direction==Direction.NORTH) { 75 newY--; 76 }else if(direction==Direction.SOUTH) { 77 newY++; 78 }else if(direction==Direction.DOWN) { 79 newZ--; 80 }else if(direction==Direction.UP) { 81 newZ++; 82 } 83 if(getActionMap().isFree(this,newX,newY,newZ)) { 84 return true; 85 } 86 }else { 87 if(movingAction.getDirection()==direction) { 88 return true; 89 } 90 } 91 return false; 92 } 93 94 public boolean update() { 95 boolean bUpdated = false; 96 if(bMoving) { 97 movingProgress+=movingSpeed; 98 if(movingProgress>=MOVING_PROGRESS_MAX) { 99 getActionMap().endActionOnObject(this,movingAction); 100 bMoving = false; 101 } 102 bUpdated = true; 103 } 104 if(bDropping) { 105 droppingProgress+=droppingSpeed; 106 LOG.debug(getMovingProgressZ()+","+droppingProgress+","+droppingHeight); 107 if(getMovingProgressZ()<=-1) { 108 getActionMap().endActionOnObject(this,Action.DROP); 109 droppingHeight++; 110 bDropping=false; 111 } 112 bUpdated = true; 113 }else { 114 droppingHeight=0; 115 droppingProgress=0; 116 } 117 if(!bDropping) { 118 ActionInterface a = getActionMap().startActionOnObject(this,Action.DROP); 119 if(a==Action.DROP) { 120 LOG.debug("dropping from "+getPosZ()); 121 bDropping = true; 122 droppingSpeed = 1f/DROPPING_PROGRESS_MAX; 123 bUpdated = true; 124 } 125 } 126 return super.update()?true:bUpdated; 127 } 128 protected float getMovingProgressX() { 129 if(bMoving) { 130 if(movingAction.getDirection()==Direction.WEST) { 131 return -(float)movingProgress/MOVING_PROGRESS_MAX; 132 }else if(movingAction.getDirection()==Direction.EAST) { 133 return (float)movingProgress/MOVING_PROGRESS_MAX; 134 } 135 } 136 return 0; 137 } 138 protected float getMovingProgressY() { 139 if(bMoving) { 140 if(movingAction.getDirection()==Direction.NORTH) { 141 return -(float)movingProgress/MOVING_PROGRESS_MAX; 142 }else if(movingAction.getDirection()==Direction.SOUTH) { 143 return (float)movingProgress/MOVING_PROGRESS_MAX; 144 } 145 } 146 return 0; 147 } 148 protected float getMovingProgressZ() { 149 if(bDropping) { 150 return -(float)((droppingProgress)*(droppingProgress)-(droppingHeight)); 151 } 152 return 0; 153 } 154 public int getDrawingPosX(float dx, float dy, float dz) { 155 return super.getDrawingPosX(dx+getMovingProgressX(),dy+getMovingProgressY(),dz+getMovingProgressZ()); 156 } 157 158 public int getDrawingPosY(float dx, float dy, float dz) { 159 return super.getDrawingPosY(dx+getMovingProgressX(),dy+getMovingProgressY(),dz+getMovingProgressZ()); 160 } 161 public int getPixelPosX() { 162 return getContainer().getPositionX(getPosX()+getMovingProgressX(),getPosY()+getMovingProgressY(),getPosZ()+getMovingProgressZ()); 163 } 164 public int getPixelPosY() { 165 return getContainer().getPositionY(getPosX()+getMovingProgressX(),getPosY()+getMovingProgressY(),getPosZ()+getMovingProgressZ()); 166 } 167 168 public boolean isMoving() { 169 return bMoving; 170 } 171 172 protected boolean isDropping() { 173 return bDropping; 174 } 175 176 public float getMovingPosX() { 177 if(isMoving() && getMovingProgressX()!=0) { 178 return (int)(getPosX()+getMovingProgressX()/Math.abs(getMovingProgressX())); 179 }else { 180 return getPosX(); 181 } 182 } 183 184 public float getMovingPosY() { 185 if(isMoving() && getMovingProgressY()!=0) { 186 return (int)(getPosY()+getMovingProgressY()/Math.abs(getMovingProgressY())); 187 }else { 188 return getPosY(); 189 } 190 } 191 192 public float getMovingPosZ() { 193 if(isMoving() && getMovingProgressZ()!=0) { 194 return (int)(getPosZ()+getMovingProgressZ()/Math.abs(getMovingProgressZ())); 195 }else { 196 return getPosZ(); 197 } 198 } 199 */ 200 }