Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
Tiles.java
Go to the documentation of this file.
1 // This file is part of MicropolisJ.
2 // Copyright (C) 2013 Jason Long
3 // Portions Copyright (C) 1989-2007 Electronic Arts Inc.
4 //
5 // MicropolisJ is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GPLv3, with additional terms.
7 // See the README file, included in this distribution, for details.
8 
9 package micropolisj.engine;
10 
11 import java.io.*;
12 import java.nio.charset.Charset;
13 import java.util.*;
14 
18 public class Tiles {
19  static final Charset UTF8 = Charset.forName("UTF-8");
20  static TileSpec[] tiles;
21  static Map<String, TileSpec> tilesByName = new HashMap<String, TileSpec>();
22  static {
23  try {
24  readTiles();
25  checkTiles();
26  }
27  catch(IOException e) {
28  throw new RuntimeException(e);
29  }
30  }
31 
32  static void readTiles() throws IOException {
33  ArrayList<TileSpec> tilesList = new ArrayList<TileSpec>();
34 
35  Properties tilesRc = new Properties();
36  tilesRc.load(new InputStreamReader(Tiles.class.getResourceAsStream("/tiles.rc"), UTF8));
37 
38  for(int i = 0;; i++) {
39  String tileName = Integer.toString(i);
40  String rawSpec = tilesRc.getProperty(tileName);
41  if(rawSpec == null) {
42  break;
43  }
44 
45  TileSpec ts = TileSpec.parse(i, rawSpec, tilesRc);
46  tilesByName.put(tileName, ts);
47  tilesList.add(ts);
48  }
49  tiles = tilesList.toArray(new TileSpec[0]);
50 
51  for(int i = 0; i < tiles.length; i++) {
52  tiles[i].resolveReferences(tilesByName);
53 
54  TileSpec.BuildingInfo bi = tiles[i].getBuildingInfo();
55  if(bi != null) {
56  for(int j = 0; j < bi.members.length; j++) {
57  int tid = bi.members[j];
58  int offx = (bi.width >= 3 ? -1 : 0) + j % bi.width;
59  int offy = (bi.height >= 3 ? -1 : 0) + j / bi.width;
60 
61  if(tiles[tid].owner == null && (offx != 0 || offy != 0)) {
62  tiles[tid].owner = tiles[i];
63  tiles[tid].ownerOffsetX = offx;
64  tiles[tid].ownerOffsetY = offy;
65  }
66  }
67  }
68  }
69  }
70 
77  public static TileSpec get(int tileNumber) {
78  if(tileNumber >= 0 && tileNumber < tiles.length) {
79  return tiles[tileNumber];
80  }
81  else {
82  return null;
83  }
84  }
85 
86  static void checkTiles() {
87  for(int i = 0; i < tiles.length; i++) {
88  // do something here
89  }
90  }
91 }