map.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package tile
  2. // import "fmt"
  3. // import "log"
  4. import "gitlab.com/beoran/al5go/al"
  5. // import "gitlab.com/beoran/ebsgo/engine/geometry/point"
  6. import "gitlab.com/beoran/ebsgo/engine/physics"
  7. // import "gitlab.com/beoran/ebsgo/engine/fifi"
  8. /** A tile map is a tile based map, roughly equivalent to a
  9. * "level" that uses tiled panes for it's display.
  10. */
  11. type Map struct {
  12. Background *al.Bitmap
  13. Panes []*Pane
  14. Sets []*Set
  15. GridW int
  16. GridH int
  17. }
  18. const (
  19. TEXTURE_TILE = "tile"
  20. TEXTURE_PLAYER = "play"
  21. TEXTURE_GEAR = "gear"
  22. TEXTURE_WEAPON = "arms"
  23. TEXTURE_FOE = "foes"
  24. TEXTURE_ITEM = "item"
  25. )
  26. /** Initializes a tile map */
  27. func (tm * Map) Init(tw, th int) {
  28. tm.GridW = tw
  29. tm.GridH = th
  30. tm.Panes = nil
  31. }
  32. func NewMap(tw, th int) * Map{
  33. tm := &Map{}
  34. tm.Init(tw, th)
  35. return tm
  36. }
  37. /** Returns a pointer to the pane at index or NULL if out of range. */
  38. func (tm * Map) Pane(index int) * Pane {
  39. if index < 0 { return nil; }
  40. if index >= len(tm.Panes) { return nil; }
  41. return tm.Panes[index]
  42. }
  43. func (tm * Map) AddPane(pane * Pane) * Pane {
  44. tm.Panes = append(tm.Panes, pane)
  45. return pane
  46. }
  47. func (tm * Map) NewPane(set * Set, tw, th int, name string) * Pane {
  48. pane := NewPane(set, tw, th, name)
  49. return tm.AddPane(pane)
  50. }
  51. /** Returns a pointer to the pane at index or NULL if out of range. */
  52. func (tm * Map) Set(index int) * Set {
  53. if index < 0 { return nil; }
  54. if index >= len(tm.Sets) { return nil; }
  55. return tm.Sets[index]
  56. }
  57. func (tm * Map) AddSet(set * Set) * Set {
  58. tm.Sets = append(tm.Sets, set)
  59. return set
  60. }
  61. func (tm * Map) NewSet(sheet * al.Bitmap, tile_w, tile_h, firstgid int) * Set {
  62. set := NewSet(sheet, tile_w, tile_h, firstgid)
  63. return tm.AddSet(set)
  64. }
  65. /* Looks up the tile set to use for the tile, based on it's
  66. * TMX index. this is done based on the firstgid varables of the tile set.
  67. * In case there are several matches the first mathing tile set is returned
  68. */
  69. func (tm * Map) LookupTmxTileset(tmx_index int) * Set{
  70. for _, set := range tm.Sets {
  71. if tmx_index >= set.FirstGID {
  72. return set
  73. }
  74. }
  75. return nil
  76. }
  77. /** Returns the tile in the tile map in the given layer at the given tile coords. */
  78. func (tm * Map) Tile(l, x, y int) * Tile {
  79. pane := tm.Pane(l)
  80. if pane == nil { return nil; }
  81. return pane.Tile(x, y)
  82. }
  83. /** Sets a tile in the tile map to the given tile. */
  84. func (tm * Map) SetTile(l, x, y int, tile * Tile) * Tile {
  85. pane := tm.Pane(l)
  86. if pane == nil { return nil }
  87. return pane.SetTile(x, y, tile)
  88. }
  89. /** Sets a tile in the tile map to the tile with the given index. */
  90. func (tm * Map) SetTileIndex(l, x, y, index int) * Tile {
  91. pane := tm.Pane(l)
  92. if pane == nil { return nil }
  93. return pane.SetTileIndex(x, y, index)
  94. }
  95. /** Draws a tile map. */
  96. func (tm * Map) Draw(camera * physics.Camera) {
  97. // var floor * Pane
  98. for i := 0 ; i < len(tm.Panes) ; i ++ {
  99. pane := tm.Panes[i]
  100. if pane != nil {
  101. pane.DrawTiles(camera)
  102. if ( i % 2 ) == 0 {
  103. // pane.DrawBlends(camera)
  104. // floor = pane
  105. } else if (i % 2) == 1 {
  106. // pane.DrawShadowsOn(floor, camera)
  107. }
  108. }
  109. }
  110. }
  111. /** Updates the tile map. Currently this animates the tiles. */
  112. func (tm * Map) Update(dt float64) {
  113. for i := 0 ; i < len(tm.Panes) ; i ++ {
  114. pane := tm.Panes[i]
  115. if pane != nil {
  116. pane.Update(dt)
  117. }
  118. }
  119. }
  120. /** Sets up the camera so it will stay locked in to the
  121. given layer of the tile map */
  122. /*
  123. Lockin * tilepane_lockin(Tilepane * pane, Camera * camera) {
  124. float x, y, w, h;
  125. if(!pane) return NULL;
  126. x = 0.0;
  127. y = 0.0;
  128. w = tilepane_wide(pane);
  129. h = tilepane_high(pane);
  130. return camera_newlockin(camera, x, y, w, h);
  131. }
  132. Lockin * tilemap_layer_lockin(Tilemap * map,
  133. int layer,
  134. Camera * camera) {
  135. Tilepane * pane;
  136. Lockin * result;
  137. float x, y, w, h;
  138. if (!map) return NULL;
  139. pane = tilemap_pane(map, layer);
  140. return tilepane_lockin(pane, camera);
  141. }
  142. Area * tilemap_area(Tilemap * self) {
  143. if(!self) return NULL;
  144. return self->area;
  145. }
  146. Thing * tilemap_thing(Tilemap * self, int index) {
  147. Area * area = tilemap_area(self);
  148. return area_thing(area, index);
  149. }
  150. */
  151. func (tm * Map) SetupAfterLoad() {
  152. for i := 0 ; i < len(tm.Panes) ; i ++ {
  153. pane := tm.Panes[i]
  154. if pane != nil {
  155. pane.InitBlend(i)
  156. }
  157. }
  158. }