View Javadoc

1   package erland.game.tileadventure.isodiamond;
2   
3   import erland.game.tileadventure.IrregularBlockContainerInterface;
4   import erland.game.tileadventure.BlockContainerData;
5   
6   /*
7    * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com)
8    *
9    * This program is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; either version 2
12   * of the License, or (at your option) any later version.
13   *
14   * This program 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
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with this program; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22   * 
23   */
24  
25  public class IsoDiamondBlockContainerData extends BlockContainerData {
26      private int blockDrawingOffsetX;
27      private int blockDrawingOffsetY;
28      public IsoDiamondBlockContainerData(int offsetX, int offsetY, int blockDrawingOffsetX, int blockDrawingOffsetY, int sizeX, int sizeY,int sizeZ, int squareSizeX,int squareSizeY,int squareSizeZ, int visibleSizeX, int visibleSizeY) {
29          super(offsetX,offsetY,sizeX,sizeY,sizeZ, squareSizeX,squareSizeY,squareSizeZ,visibleSizeX,visibleSizeY);
30          this.blockDrawingOffsetX = blockDrawingOffsetX;
31          this.blockDrawingOffsetY = blockDrawingOffsetY;
32      }
33  
34      public int getDrawingPositionX(float x, float y, float z)
35      {
36          return getOffsetX() + blockDrawingOffsetX - getScrollingOffsetX() + getPositionX(x,y,z);
37      }
38      public int getDrawingPositionY(float x, float y, float z)
39      {
40          return getOffsetY() + blockDrawingOffsetY - getScrollingOffsetY() + getPositionY(x,y,z);
41      }
42      public int getPositionX(float x, float y, float z)
43      {
44      	return ((getSquareSizeX()*getSizeX())>>1)-(getSquareSizeX()>>1)+(int)((getSquareSizeX()>>1)*x-(getSquareSizeX()>>1)*y);
45      }
46      public int getPositionY(float x, float y, float z)
47      {
48          return (int)((getSquareSizeY()>>1)*x+(getSquareSizeY()>>1)*y-getSquareSizeZ()*z);
49      }
50      public boolean getVisible(float posX, float posY,float posZ)
51      {
52          if((getPositionX(posX,posY,posZ)+getSquareSizeX())>getScrollingOffsetX() && getPositionX(posX,posY,posZ)<(getScrollingOffsetX()+getDrawingSizeX())) {
53              if((getPositionY(posX,posY,posZ)+getSquareSizeY())>getScrollingOffsetY() && getPositionY(posX,posY,posZ)<(getScrollingOffsetY()+getDrawingSizeY())) {
54                  return true;
55              }
56          }
57          return false;
58      }
59  
60      public int getPixelDrawingPositionX(float x, float y, float z) {
61          return getOffsetX() - getScrollingOffsetX() + getPositionX(x,y,z) + getSquareSizeX()/2;
62      }
63  
64      public int getPixelDrawingPositionY(float x, float y, float z) {
65          return getOffsetY() - getScrollingOffsetY() + getPositionY(x,y,z);
66      }
67      /***
68  	 * Get the horizontal scrolling size
69  	 * @return The horizontal scrolling offset (Pixel coordinates)
70  	 */
71  	public int getScrollingSizeX()
72  	{
73  		return getSizeX()*getSquareSizeX()+getSizeY()*getSquareSizeX();
74  	}
75  
76      /***
77  	 * Get the vertical scrolling size
78  	 * @return The vertical scrolling size (Pixel coordinates)
79  	 */
80  	public int getScrollingSizeY()
81  	{
82  		return getSizeX()*getSquareSizeY()+getSizeY()*getSquareSizeY()+getSizeZ()*getSquareSizeZ();
83  	}
84  
85      public int getBlockPositionX(int x, int y, int z) {
86          int realX = x+getScrollingOffsetX()-getOffsetX();
87          int realY = y+getScrollingOffsetY()-getOffsetY();
88          int zeroX = getPositionX(0,0,(float)z/getSquareSizeZ())+getSquareSizeX()/2;
89          int zeroY = getPositionY(0,0,(float)z/getSquareSizeZ());
90  
91          int screenDeltaX = realX-zeroX;
92          int screenDeltaY = realY-zeroY;
93  
94          int worldDeltaX = (screenDeltaX+screenDeltaY*2)/getSquareSizeX();
95          int worldDeltaY = (screenDeltaY*2-screenDeltaX)/getSquareSizeX();
96  
97          int posX = worldDeltaX;
98          int posY = worldDeltaY;
99  
100         return posX;
101     }
102 
103     public int getBlockPositionY(int x, int y, int z) {
104         int realX = x+getScrollingOffsetX()-getOffsetX();
105         int realY = y+getScrollingOffsetY()-getOffsetY();
106         int zeroX = getPositionX(0,0,(float)z/getSquareSizeZ())+getSquareSizeX()/2;
107         int zeroY = getPositionY(0,0,(float)z/getSquareSizeZ());
108 
109         int screenDeltaX = realX-zeroX;
110         int screenDeltaY = realY-zeroY;
111 
112         int worldDeltaX = (screenDeltaX+screenDeltaY*2)/getSquareSizeX();
113         int worldDeltaY = (screenDeltaY*2-screenDeltaX)/getSquareSizeX();
114 
115         int posX = worldDeltaX;
116         int posY = worldDeltaY;
117 
118         return posY;
119     }
120 }