MazeCell.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2015 iZc
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package de.nplusc.iZc.MazeGame.cells;
  18. import de.nplusc.iZc.MazeGame.Objects.Player;
  19. import java.awt.Image;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import javax.imageio.ImageIO;
  25. /**
  26. *
  27. * @author LH
  28. */
  29. public abstract class MazeCell
  30. {
  31. public static final int DIRECTION_NORTH=0;
  32. public static final int DIRECTION_EAST=1;
  33. public static final int DIRECTION_SOUTH=2;
  34. public static final int DIRECTION_WEST=3;
  35. private Map<Integer,boolean[]> exits = new HashMap<>();//ints for layer and bools for directions
  36. private Map<Integer,boolean[]> entrances = new HashMap<>();//ints for layer and bools for directions
  37. //entrance but no exit ->onewaydoor
  38. private String TexturePath = "";
  39. private int direction=0;
  40. private int baseLayer=0,layerHeight=1; //wichtig bei stacked-tiles wie brücken, tunnels;
  41. public MazeCell(Map<Integer,boolean[]> pexits, Map<Integer,boolean[]> pentrances,int bl,int lh)
  42. {
  43. exits = pexits; entrances = pentrances;
  44. }
  45. public MazeCell(Map<Integer,boolean[]> pexits, Map<Integer,boolean[]> pentrances,int bl)
  46. {
  47. this(pexits,pentrances,bl,1);
  48. }
  49. public MazeCell()
  50. {
  51. }
  52. public boolean canExit(int direction,int layer,Player p)
  53. {
  54. if(!exits.containsKey(layer-baseLayer))
  55. {
  56. return false;
  57. }
  58. return exits.get(layer-baseLayer)[direction];
  59. }
  60. public boolean canEnter(int direction,int layer,Player p)
  61. {
  62. if(!entrances.containsKey(layer-baseLayer))
  63. {
  64. return false;
  65. }
  66. return entrances.get(layer-baseLayer)[direction];
  67. }
  68. public int getDirection() //setter ist serialisierungskram also nach unten verbannt
  69. {
  70. return direction;
  71. }
  72. //setTileImage() ->setTexturePath()
  73. public Image getTextureTile()
  74. {
  75. Image i = null;
  76. try
  77. {
  78. i = ImageIO.read(new File(TexturePath));
  79. }
  80. catch (IOException ex)
  81. {
  82. ex.printStackTrace();
  83. }
  84. return i;
  85. }
  86. int getExitLayer(int direction) //geht da nur maximal ein exit pro richtung möglich
  87. {
  88. int ret = Integer.MIN_VALUE; //integer_minimum == kein exit in diese richtung
  89. Integer[] layers = exits.keySet().toArray(new Integer[]{0});
  90. for (int i = 0; i < layers.length; i++)
  91. {
  92. int k = layers[i];
  93. if(exits.get(k)[direction])
  94. {
  95. return k;
  96. }
  97. }
  98. return ret; //fallback bei kein exit verfügbar
  99. }
  100. //Serialisuerungskram#
  101. public int getBaseLayer()
  102. {
  103. return baseLayer;
  104. }
  105. public void setBaseLayer(int baseLayer)
  106. {
  107. this.baseLayer = baseLayer;
  108. }
  109. public int getLayerHeight()
  110. {
  111. return layerHeight;
  112. }
  113. public void setLayerHeight(int layerHeight)
  114. {
  115. this.layerHeight = layerHeight;
  116. }
  117. public void setDirection(int dir)
  118. {
  119. direction=dir;
  120. }
  121. public Map<Integer, boolean[]> getExits()
  122. {
  123. return exits;
  124. }
  125. public void setExits(Map<Integer, boolean[]> exits)
  126. {
  127. this.exits = exits;
  128. }
  129. public Map<Integer, boolean[]> getEntrances()
  130. {
  131. return entrances;
  132. }
  133. public void setEntrances(Map<Integer, boolean[]> entrances)
  134. {
  135. this.entrances = entrances;
  136. }
  137. public String getTexturePath()
  138. {
  139. return TexturePath;
  140. }
  141. public void setTexturePath(String TexturePath)
  142. {
  143. this.TexturePath = TexturePath;
  144. }
  145. public abstract String onCellEnter(Player p); //interface für msgs
  146. public boolean requiresFOV() // ob aufgedeckt oder nicht wird im player gespeichert
  147. {
  148. return true;
  149. }
  150. }