View Javadoc

1   package erland.game.tileadventure.isodiamond;
2   
3   import erland.game.tileadventure.*;
4   
5   import java.awt.*;
6   import java.util.List;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  
11  /*
12   * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com)
13   *
14   * This program is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU General Public License
16   * as published by the Free Software Foundation; either version 2
17   * of the License, or (at your option) any later version.
18   *
19   * This program is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   * GNU General Public License for more details.
23   *
24   * You should have received a copy of the GNU General Public License
25   * along with this program; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27   * 
28   */
29  
30  public class IsoDiamondDrawMap extends DrawMap {
31      /*** Logging instance */
32      private static Log LOG = LogFactory.getLog(LivingObject.class);
33  
34      public void draw(Graphics g) {
35          BitmapObject.drawCount = 0;
36          MapObjectContainerInterface[] maps = getMaps();
37          if(maps.length>0) {
38              MapObjectContainerInterface main = (MapObjectContainerInterface) maps[0];
39              int noOfRows = main.getSizeX();
40              int prevX = 0;
41              int prevY = 0;
42              for(int row=0;row<noOfRows;row++) {
43                  for(int x=0,y=row;y>=0;y--,x++) {
44                      boolean bRedraw = false;
45                      for(int z=0;z<main.getSizeZ();z++) {
46                          drawTile(maps, x, y, z, g);
47                          GameObject block = (GameObject) main.getBlock(x,y,z);
48                          GameObject drawnBlock = (GameObject) main.getBlock(x-1,y,z);
49                          if(!bRedraw && drawnBlock!=null && block==drawnBlock) {
50                              bRedraw = true;
51                          }
52                          List objects = main.getObjects(x,y,z);
53                          List drawnObjects = main.getObjects(x-1,y,z);
54                          for(int i=0;!bRedraw&&drawnObjects!=null && i<drawnObjects.size();i++) {
55                              GameObject drawnObject = (GameObject) drawnObjects.get(i);
56                              for(int j=0;!bRedraw&&objects!=null && j<objects.size();j++) {
57                                  GameObject object = (GameObject) objects.get(j);
58                                  //System.out.println("Checking "+drawnObject+" against "+object);
59                                  if(object==drawnObject) {
60                                      bRedraw=true;
61                                      break;
62                                  }
63                              }
64                          }
65                          /*
66                          GameObject obj = (GameObject)main.getBlock(x,y,z);
67                          if(!bRedraw && obj!=null && (obj.getPosX()>obj.getMovingPosX())) {
68                              bRedraw = true;
69                          }else if(!bRedraw) {
70                              for(int i=1;i<maps.length;i++) {
71                                  MapObjectContainerInterface map = (MapObjectContainerInterface) maps[i];
72                                  obj = (GameObject) map.getBlock(x,y,z);
73                                  if(!bRedraw && obj!=null && (obj.getPosX()<obj.getMovingPosX())) {
74                                      bRedraw = true;
75                                      break;
76                                  }
77                              }
78                          }
79                          */
80                      }
81                      if(bRedraw) {
82                          //System.out.println("*************Redraw ("+prevX+","+prevY+")******************");
83                          for(int z=0;z<main.getSizeZ();z++) {
84                              drawTile(maps, prevX, prevY, z, g,true);
85                          }
86                      }
87                      prevX = x;
88                      prevY = y;
89                  }
90              }
91              int noOfColumns = main.getSizeY()-1;
92              for(int column=1;column<=noOfColumns;column++) {
93                  for(int x=column,y=noOfColumns;y>=column;y--,x++) {
94                      boolean bRedraw = false;
95                      for(int z=0;z<main.getSizeZ();z++) {
96                          drawTile(maps, x, y, z, g);
97                          GameObject block = (GameObject) main.getBlock(x,y,z);
98                          GameObject drawnBlock = (GameObject) main.getBlock(x-1,y,z);
99                          if(!bRedraw && drawnBlock!=null && block==drawnBlock) {
100                             bRedraw = true;
101                         }
102                         List objects = main.getObjects(x,y,z);
103                         List drawnObjects = main.getObjects(x-1,y,z);
104                         for(int i=0;!bRedraw&&drawnObjects!=null && i<drawnObjects.size();i++) {
105                             GameObject drawnObject = (GameObject) drawnObjects.get(i);
106                             for(int j=0;!bRedraw&&objects!=null && j<objects.size();j++) {
107                                 GameObject object = (GameObject) objects.get(j);
108                                 //System.out.println("Checking "+drawnObject+" against "+object);
109                                 if(object==drawnObject) {
110                                     bRedraw=true;
111                                     break;
112                                 }
113                             }
114                         }
115                         /*
116                         GameObject obj = (GameObject) main.getBlock(x,y,z);
117                         if(!bRedraw && obj!=null && (obj.getPosX()>obj.getMovingPosX() && obj.getPosX()>prevX)) {
118                             bRedraw = true;
119                         }else if(!bRedraw) {
120                             for(int i=1;i<maps.length;i++) {
121                                 MapObjectContainerInterface map = (MapObjectContainerInterface) maps[i];
122                                 obj = (GameObject) map.getBlock(x,y,z);
123                                 if(!bRedraw && obj!=null && (obj.getPosX()<obj.getMovingPosX() && obj.getMovingPosX()>prevX)) {
124                                     bRedraw = true;
125                                     break;
126                                 }
127                             }
128                         }
129                         */
130                     }
131                     if(bRedraw && prevX<x) {
132                         for(int z=0;z<main.getSizeZ();z++) {
133                             drawTile(maps, prevX, prevY, z, g,true);
134                         }
135                     }
136                     prevX = x;
137                     prevY = y;
138                 }
139             }
140         }
141         //System.out.println("Draw count:"+BitmapObject.drawCount);
142     }
143 
144     private void drawTile(MapObjectContainerInterface[] maps, int x, int y, int z, Graphics g) {
145         drawTile(maps,x,y,z,g,false);
146     }
147     private void drawTile(MapObjectContainerInterface[] maps, int x, int y, int z, Graphics g,boolean drawRect) {
148         MapObjectInterface obj = maps[0].getBlock(x,y,z);
149         if(obj!=null) {
150             obj.draw(g);
151             if(drawRect) {
152                 drawRect(g,obj);
153             }
154         }
155         List objects = maps[0].getObjects(x,y,z);
156         for(int j=0;j<objects.size();j++) {
157             obj = (MapObjectInterface) objects.get(j);
158             obj.draw(g);
159             if(drawRect) {
160                 drawRect(g,obj);
161             }
162         }
163         for(int i=1;i<maps.length;i++) {
164             MapObjectContainerInterface map = (MapObjectContainerInterface) maps[i];
165             obj = map.getBlock(x,y,z);
166             if(obj!=null) {
167                 obj.draw(g);
168                 if(drawRect) {
169                     drawRect(g,obj);
170                 }
171             }
172             objects = map.getObjects(x,y,z);
173             for(int j=0;j<objects.size();j++) {
174                 obj = (MapObjectInterface) objects.get(j);
175                 obj.draw(g);
176                 if(drawRect) {
177                     drawRect(g,obj);
178                 }
179             }
180         }
181     }
182     private void drawRect(Graphics g, MapObjectInterface obj) {
183         if(LOG.isDebugEnabled() && obj.getContainer().getVisible(obj.getPosX(),obj.getPosY(),obj.getPosZ())) {
184             Box3D box = obj.getBoundingBox();
185             int x1 = getContainer().getPixelDrawingPositionX(box.getMinX(),box.getMinY(),box.getMinZ());
186             int x2 = getContainer().getPixelDrawingPositionX(box.getMinX(),box.getMinY(),box.getMaxZ());
187             int x3 = getContainer().getPixelDrawingPositionX(box.getMinX(),box.getMaxY(),box.getMinZ());
188             int x4 = getContainer().getPixelDrawingPositionX(box.getMinX(),box.getMaxY(),box.getMaxZ());
189             int x5 = getContainer().getPixelDrawingPositionX(box.getMaxX(),box.getMinY(),box.getMinZ());
190             int x6 = getContainer().getPixelDrawingPositionX(box.getMaxX(),box.getMinY(),box.getMaxZ());
191             int x7 = getContainer().getPixelDrawingPositionX(box.getMaxX(),box.getMaxY(),box.getMinZ());
192             int x8 = getContainer().getPixelDrawingPositionX(box.getMaxX(),box.getMaxY(),box.getMaxZ());
193 
194             int y1 = getContainer().getPixelDrawingPositionY(box.getMinX(),box.getMinY(),box.getMinZ());
195             int y2 = getContainer().getPixelDrawingPositionY(box.getMinX(),box.getMinY(),box.getMaxZ());
196             int y3 = getContainer().getPixelDrawingPositionY(box.getMinX(),box.getMaxY(),box.getMinZ());
197             int y4 = getContainer().getPixelDrawingPositionY(box.getMinX(),box.getMaxY(),box.getMaxZ());
198             int y5 = getContainer().getPixelDrawingPositionY(box.getMaxX(),box.getMinY(),box.getMinZ());
199             int y6 = getContainer().getPixelDrawingPositionY(box.getMaxX(),box.getMinY(),box.getMaxZ());
200             int y7 = getContainer().getPixelDrawingPositionY(box.getMaxX(),box.getMaxY(),box.getMinZ());
201             int y8 = getContainer().getPixelDrawingPositionY(box.getMaxX(),box.getMaxY(),box.getMaxZ());
202 
203             g.setColor(Color.RED);
204             g.drawLine(x1,y1,x2,y2);
205             g.drawLine(x3,y3,x4,y4);
206             g.drawLine(x1,y1,x3,y3);
207             g.drawLine(x2,y2,x4,y4);
208 
209             g.setColor(Color.RED);
210             g.drawLine(x5,y5,x6,y6);
211             g.drawLine(x7,y7,x8,y8);
212             g.drawLine(x5,y5,x7,y7);
213             g.drawLine(x6,y6,x8,y8);
214 
215             g.setColor(Color.RED);
216             g.drawLine(x1,y1,x5,y5);
217             g.drawLine(x2,y2,x6,y6);
218             g.drawLine(x3,y3,x7,y7);
219             g.drawLine(x4,y4,x8,y8);
220         }
221     }
222 }
223