WorldSim  inDev
2D tile-based sandbox RPG with procedurally generated fantasy world simulator 🌏
World_Viewer_Minimap.hpp
Go to the documentation of this file.
1 #pragma once
2 #ifndef WORLDSIM_WORLD_VIEWER_MINIMAP_HPP
3 #define WORLDSIM_WORLD_VIEWER_MINIMAP_HPP
4 
5 /* WorldSim: Menu_WorldSimulator.hpp
6 #include "Menu_WorldSimulator.hpp" */
7 
13 #include "World_Viewer.hpp"
14 
15 #include <Graphics/PixelScreen/PixelScreen.hpp>
16 #include <Graphics/GUI/GUI_Interface.hpp>
17 
18 
19 class WorldViewer_Minimap: public GUI_Interface
20 {
21 private:
22 
23  PixelScreen minimap;
24 
25 public:
27 
28  WorldViewer_Minimap(): minimap(1,1)
29  {
30  world = 0;
31  }
32 
33  void init()
34  {
35  if (world==0)
36  { return; }
37 
38  // load up the minimap pixels here
39  minimap.init(world->nX,world->nY);
40  minimap.fill(0,0,255,255);
41 
42 
43 
44  for (int y=0;y<world->nY;++y)
45  {
46  for (int x=0;x<world->nX;++x)
47  {
48  Texture * tex = world->aWorldTile(x,y).currentTexture();
49  if ( tex )
50  {
51  minimap.setPixel(x,y,tex->averageRed,tex->averageGreen,tex->averageBlue);
52  }
53  }
54  }
55 
56  eventResize();
57  }
58 
59  void render()
60  {
61  if (world==0)
62  { return; }
63 
64  // Render the viewbox as a grey overlay on the minimap
65  // Like many things this ought to be moved out of the render loop.
66  // X axis
67  double leftMostTile = worldViewer.centerTileX - (worldViewer.getTilesNX()/2);
68  if (leftMostTile<0) {leftMostTile=0;}
69  if (leftMostTile>world->nX) {leftMostTile=world->nX;}
70 
71  // +0.5 adjustment seems to improve the precision of the edge, not sure why.
72  double rightMostTile = worldViewer.centerTileX + (worldViewer.getTilesNX()/2)+0.5;
73  if (rightMostTile<0) {rightMostTile=0;}
74  if (rightMostTile>world->nX) {rightMostTile=world->nX;}
75 
76  double rightMostTilePercent = rightMostTile / world->nX;
77  double leftMostTilePercent = leftMostTile / world->nX;
78 
79  int minimapViewboxX2 = panelX1+(panelNX*rightMostTilePercent);
80  int minimapViewboxX1 = panelX1+(panelNX*leftMostTilePercent);
81 
82  // Y axis
83  double upMostTile = worldViewer.centerTileY + (worldViewer.getTilesNY()/2)+0.5;
84  if (upMostTile<0) {upMostTile=0;}
85  if (upMostTile>world->nY) {upMostTile=world->nY;}
86 
87  double downMostTile = worldViewer.centerTileY - (worldViewer.getTilesNY()/2);
88  if (downMostTile<0) {downMostTile=0;}
89  if (downMostTile>world->nY) {downMostTile=world->nY;}
90 
91  double upMostTilePercent = upMostTile / world->nY;
92  double downMostTilePercent = downMostTile / world->nY;
93 
94  int minimapViewboxY2 = panelY1+(panelNY*upMostTilePercent);
95  int minimapViewboxY1 = panelY1+(panelNY*downMostTilePercent);
96 
97  minimap.render();
98 
99  // render viewbox over minimap
100  Renderer::placeColour4a(255,255,255,60,minimapViewboxX1,minimapViewboxY1,minimapViewboxX2,minimapViewboxY2);
101  }
102 
103  bool mouseEvent (Mouse* _mouse)
104  {
105  if (world==0)
106  { return false; }
107 
108  if (_mouse->x < panelX1 || _mouse->x>panelX2 || _mouse->y < panelY1 || _mouse->y>panelY2)
109  {
110  return false;
111  }
112 
113  // center the worldViewer on the tile we clicked in the minimap.
114  if (_mouse->isLeftClick)
115  {
116  float xMousePos = _mouse->x - panelX1;
117  float xPercent = xMousePos / panelNX;
118  int tileX = xPercent * world->nX;
119 
120  float yMousePos = _mouse->y - panelY1;
121  float yPercent = yMousePos / panelNY;
122  int tileY = yPercent * world->nY;
123 
124 
125  worldViewer.setCenterTile(tileX,tileY);
126  //_mouse->isLeftClick=false;
127  return true;
128  }
129 
130 
131 
132  return false;
133  }
134 
135  void eventResize()
136  {
137  //std::cout<<"MINIMAP RESIZED: "<<panelX1<<", "<<panelY1<<", "<<panelX2<<", "<<panelY2<<".\n";
138  int panelSizeX = panelX2-panelX1;
139  int panelSizeY = panelY2-panelY1;
140 
141  minimap.setPanel(panelX1,panelY1,panelX2,panelY2);
142  }
143 
144 };
146 
147 #endif
void render()
Definition: World_Viewer_Minimap.hpp:59
void init()
Definition: World_Viewer_Minimap.hpp:33
Definition: World.hpp:46
double centerTileX
Definition: World_Viewer.hpp:154
WorldViewer_Minimap()
Definition: World_Viewer_Minimap.hpp:28
void setCenterTile(const double _centerTileX, const double _centerTileY, const int _subTileX=LOCAL_MAP_SIZE/2, const int _subTileY=LOCAL_MAP_SIZE/2)
Definition: World_Viewer.hpp:414
WorldViewer worldViewer
Definition: World_Viewer.hpp:1214
ArrayS2< World_Local > aWorldTile
Definition: World.hpp:103
int nY
Definition: World.hpp:65
void eventResize()
Definition: World_Viewer_Minimap.hpp:135
int nX
Definition: World.hpp:65
double getTilesNX()
Definition: World_Viewer.hpp:1203
bool mouseEvent(Mouse *_mouse)
Definition: World_Viewer_Minimap.hpp:103
double getTilesNY()
Definition: World_Viewer.hpp:1208
World minimap menu Displays a minimap of the entire world. Allows player to see where they are in the...
Definition: World_Viewer_Minimap.hpp:19
World * world
Definition: World_Viewer_Minimap.hpp:26
double centerTileY
Definition: World_Viewer.hpp:154