nsnake
Classic snake game for the terminal
AnimationWater.cpp
1 #include <Display/Animations/AnimationWater.hpp>
2 #include <Engine/Graphics/Colors.hpp>
3 #include <Engine/Helpers/Utils.hpp>
4 
5 static ColorPair white;
6 static ColorPair blue = Colors::pair("blue", "default" );
7 static ColorPair blueBold = Colors::pair("blue", "default", true);
8 static ColorPair cyan = Colors::pair("cyan", "default" );
9 static ColorPair cyanBold = Colors::pair("cyan", "default", true);
10 
11 static int gray_scale_size = 11;
12 static char gray_scale[12] = "#@%#*+=-:'.";
13 
14 AnimationWater::AnimationWater(Window* window):
15  Animation(window),
16  buffer1(NULL),
17  buffer2(NULL)
18 { }
19 AnimationWater::~AnimationWater()
20 {
21  SAFE_DELETE(buffer1);
22  SAFE_DELETE(buffer2);
23 }
24 void AnimationWater::load()
25 {
26  unsigned int width = window->getW();
27  unsigned int height = window->getH();
28 
29  buffer1 = new Array2D<int>(width, height);
30  buffer2 = new Array2D<int>(width, height);
31 
32  for (unsigned int i = 0; i < width; i++)
33  {
34  for (unsigned int j = 0; j < height; j++)
35  {
36  buffer1->set(i, j, Utils::Random::between(HEIGHT_MIN,
37  HEIGHT_PERCENT(13)));
38  buffer2->set(i, j, Utils::Random::between(HEIGHT_MIN,
39  HEIGHT_PERCENT(25)));
40  }
41  }
42 
43  timer.start();
44 }
45 void AnimationWater::update()
46 {
47  // Updating only at the right time!
48  if (timer.delta_ms() < 300)
49  return;
50 
51  // Swapping the buffers
52  Array2D<int>* tmp = buffer1;
53  buffer1 = buffer2;
54  buffer2 = tmp;
55 
56  // Randomly adding a light point
57  if (Utils::Random::booleanWithChance(0.31))
58  buffer2->set(Utils::Random::between(0, buffer2->width()-1),
59  Utils::Random::between(0, buffer2->height()-1), HEIGHT_PERCENT(90));
60 
61  // Dont update the edges
62  for (unsigned int i = 1; i < (buffer1->width() - 1); i++)
63  {
64  for (unsigned int j = 1; j < (buffer1->height() - 1); j++)
65  {
66  // Instead of dividing by two,
67  // we're shifting by one
68  buffer2->set(i, j, ((buffer1->at(i-1, j) +
69  buffer1->at(i+1, j) +
70  buffer1->at(i, j+1) +
71  buffer1->at(i, j-1)) >> 1) - buffer2->at(i, j));
72 
73 // buffer2->at(i, j) -= (buffer2->at(i, j) >> 5);
74  }
75  }
76 
77  timer.start();
78 }
79 void AnimationWater::draw()
80 {
81  for (unsigned int i = 0; i < (buffer2->width()); i++)
82  {
83  for (unsigned int j = 0; j < (buffer2->height()); j++)
84  {
85  int c = ' ';
86  ColorPair p = white;
87  int s = buffer2->at(i, j);
88 
89  if (s > HEIGHT_PERCENT(80))
90  p = white;
91 
92  else if (s > HEIGHT_PERCENT(60))
93  p = cyanBold;
94 
95  else if (s > HEIGHT_PERCENT(40))
96  p = cyan;
97 
98  else if (s > HEIGHT_PERCENT(20))
99  p = blue;
100 
101  else
102  p = blueBold;
103 
104  if ((s > HEIGHT_MAX) || (s < HEIGHT_MIN))
105  continue;
106 
107  else
108  c = gray_scale[(s - HEIGHT_MIN) * (gray_scale_size-1)/HEIGHT_MAX];
109 
110  window->printChar(c, i, j, p);
111  }
112  }
113 }
114