View Javadoc

1   package erland.game.tileadventure;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   
6   import java.awt.*;
7   
8   /*
9    * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com)
10   *
11   * This program is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU General Public License
13   * as published by the Free Software Foundation; either version 2
14   * of the License, or (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU General Public License
22   * along with this program; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24   *
25   */
26  
27  public class LivingObject extends MovableObject {
28      /*** Logging instance */
29      private static Log LOG = LogFactory.getLog(LivingObject.class);
30      private boolean bJumping;
31      private final static int JUMPING_PROGRESS_PER_LEVEL_MAX=15;
32      private float jumpingProgress;
33      private ActionInterface jumpingAction;
34      private final static int JUMPING_HEIGHT=2;
35      private int lastJumpingProgress;
36      private float jumpingSpeed;
37      private double jumpingEnd;
38  /*
39      protected Action isActionPossible(Action action) {
40          if(!super.isMoving()) {
41              if(!bJumping && !isDropping()) {
42                  if(action==Action.JUMP && !getActionMap().isFree(this,(int)getPosX(),(int)getPosY(),(int)getPosZ()-1)) {
43                      return getActionMap().isActionPossibleOnObject(this,action);
44                  }else {
45                      return super.isActionPossible(action);
46                  }
47              }else {
48                  return super.isActionPossible(action);
49              }
50          }
51          return Action.NONE;
52      }
53      protected boolean actionChild(Action action) {
54          if(!super.isMoving()) {
55              if(!bJumping && !isDropping()) {
56                  if(action==Action.JUMP && !getActionMap().isFree(this,(int)getPosX(),(int)getPosY(),(int)getPosZ()-1)) {
57                      ActionInterface a = getActionMap().startActionOnObject(this,action);
58                      LOG.debug("Starting JUMP trying");
59                      if(a!=null && a!=Action.NONE) {
60                          LOG.debug("Starting JUMP success");
61                          bJumping = true;
62                          jumpingProgress = 0;
63                          lastJumpingProgress =0;
64                          jumpingAction = a;
65                          jumpingEnd = Math.sqrt(JUMPING_HEIGHT);
66                          jumpingSpeed = (float)jumpingEnd/(JUMPING_HEIGHT*(JUMPING_PROGRESS_PER_LEVEL_MAX-1));
67                          LOG.debug("jumpingSpeed="+jumpingSpeed);
68                          return true;
69                      }
70                  }else {
71                      return super.actionChild(action);
72                  }
73              }else {
74                  return super.actionChild(action);
75              }
76          }
77          return true;
78      }
79  
80      public boolean update() {
81          boolean bUpdated = false;
82          if(!bJumping) {
83              bUpdated = super.update();
84          }else {
85              LOG.debug(getMovingProgressZ()+","+lastJumpingProgress);
86              jumpingProgress+=jumpingSpeed;
87              if(jumpingProgress>=jumpingEnd) {
88                  getActionMap().endActionOnObject(this,jumpingAction);
89                  LOG.debug("Jumping finished "+getPosZ());
90                  bJumping = false;
91                  bUpdated = true;
92              }else if(lastJumpingProgress!=(int)getJumpingProgress()) {
93                  getActionMap().endActionOnObject(this,jumpingAction);
94                  lastJumpingProgress=(int)getJumpingProgress();
95                  ActionInterface a = getActionMap().startActionOnObject(this,jumpingAction);
96                  if(a==jumpingAction) {
97                      LOG.debug("Jumping, next block "+getPosZ()+" allowed");
98                  }else {
99                      LOG.debug("Jumping, next block not allowed");
100                     bJumping=false;
101                 }
102                 bUpdated = true;
103             }
104             if(isMoving()) {
105                 bUpdated = super.update()?true:bUpdated;
106             }
107         }
108         return bUpdated;
109     }
110 
111     protected float getJumpingProgress() {
112         return (float)(2f*jumpingProgress*Math.sqrt(JUMPING_HEIGHT)-jumpingProgress*jumpingProgress);
113     }
114     protected float getMovingProgressZ() {
115         if(!bJumping) {
116             return super.getMovingProgressZ();
117         }else {
118             return getJumpingProgress()-lastJumpingProgress;
119         }
120     }
121 
122     public float getMovingPosZ() {
123         if(bJumping) {
124             return super.getMovingPosZ()+(int)getJumpingProgress()-lastJumpingProgress;
125         }else {
126             return super.getMovingPosZ();
127         }
128     }
129     */
130     public void draw(Graphics g) {
131         super.draw(g);    //To change body of overridden methods use File | Settings | File Templates.
132         if(LOG.isDebugEnabled()) {
133             if(getContainer().getVisible(getPosX(),getPosY(),getPosZ())) {
134                 Box3D box = getBoundingBox();
135                 int x1 = getContainer().getPixelDrawingPositionX(box.getMinX(),box.getMinY(),box.getMinZ());
136                 int x2 = getContainer().getPixelDrawingPositionX(box.getMinX(),box.getMinY(),box.getMaxZ());
137                 int x3 = getContainer().getPixelDrawingPositionX(box.getMinX(),box.getMaxY(),box.getMinZ());
138                 int x4 = getContainer().getPixelDrawingPositionX(box.getMinX(),box.getMaxY(),box.getMaxZ());
139                 int x5 = getContainer().getPixelDrawingPositionX(box.getMaxX(),box.getMinY(),box.getMinZ());
140                 int x6 = getContainer().getPixelDrawingPositionX(box.getMaxX(),box.getMinY(),box.getMaxZ());
141                 int x7 = getContainer().getPixelDrawingPositionX(box.getMaxX(),box.getMaxY(),box.getMinZ());
142                 int x8 = getContainer().getPixelDrawingPositionX(box.getMaxX(),box.getMaxY(),box.getMaxZ());
143 
144                 int y1 = getContainer().getPixelDrawingPositionY(box.getMinX(),box.getMinY(),box.getMinZ());
145                 int y2 = getContainer().getPixelDrawingPositionY(box.getMinX(),box.getMinY(),box.getMaxZ());
146                 int y3 = getContainer().getPixelDrawingPositionY(box.getMinX(),box.getMaxY(),box.getMinZ());
147                 int y4 = getContainer().getPixelDrawingPositionY(box.getMinX(),box.getMaxY(),box.getMaxZ());
148                 int y5 = getContainer().getPixelDrawingPositionY(box.getMaxX(),box.getMinY(),box.getMinZ());
149                 int y6 = getContainer().getPixelDrawingPositionY(box.getMaxX(),box.getMinY(),box.getMaxZ());
150                 int y7 = getContainer().getPixelDrawingPositionY(box.getMaxX(),box.getMaxY(),box.getMinZ());
151                 int y8 = getContainer().getPixelDrawingPositionY(box.getMaxX(),box.getMaxY(),box.getMaxZ());
152 
153                 g.setColor(Color.WHITE);
154                 g.drawLine(x1,y1,x2,y2);
155                 g.drawLine(x3,y3,x4,y4);
156                 g.drawLine(x1,y1,x3,y3);
157                 g.drawLine(x2,y2,x4,y4);
158 
159                 g.setColor(Color.WHITE);
160                 g.drawLine(x5,y5,x6,y6);
161                 g.drawLine(x7,y7,x8,y8);
162                 g.drawLine(x5,y5,x7,y7);
163                 g.drawLine(x6,y6,x8,y8);
164 
165                 g.setColor(Color.WHITE);
166                 g.drawLine(x1,y1,x5,y5);
167                 g.drawLine(x2,y2,x6,y6);
168                 g.drawLine(x3,y3,x7,y7);
169                 g.drawLine(x4,y4,x8,y8);
170             }
171         }
172     }
173 }