-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.h
151 lines (114 loc) · 4.02 KB
/
grid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#pragma once
#include<vector>
#include"raylib.h"
#include"raymath.h"
#include"constants.h"
#include"storage.h"
#include"button.h"
struct Grid{
Texture2D& texture;
Rectangle source{};
int width;
int height;
std::vector<Rectangle> tiles;
int lastHovered{-1};
int tileRise{-10};
// wave properties
bool waveState{false};
int amplitude{50};
int speed{5};
float wavelength{1000.0f};
float dt{0.0f};
// transition into/out of wave
int transitioning{0}; // turning on 1, turning off -1
float transitionFactor{0.0f};
float transitionSpeed{0.04f};
Grid(int width, int height);
void mouseHover(const Vector2& mousePos);
float waveOffset(const Rectangle& tile);
void getKeyboardInput();
void draw(const Vector2& mousePos);
// button functions
void waveSwitch(){
transitioning = waveState ? -1 : 1;
waveState = !waveState;
}
void incAmplitude(){ amplitude++; }
void decAmplitude(){ amplitude--; }
void incWavelength(){ wavelength++; }
void decWavelength(){ wavelength--; }
void incSpeed(){ speed++; }
void decSpeed(){ speed--; }
};
Grid::Grid(int width, int height): texture(textureStore.add("sprites/tile.png")), width(width), height(height){
// set size
source.width = static_cast<float>(texture.width);
source.height = static_cast<float>(texture.height);
// create grid with initial positions
Vector3 temp;
for (int j=0; j<height; j++){
for (int i=0; i<width; i++){
temp = Vector3Transform({static_cast<float>(i)*scale, static_cast<float>(j)*scale, 1}, toIso);
tiles.push_back({temp.x,temp.y,scale,scale});
}
}
}
void Grid::mouseHover(const Vector2& mousePos){
// turn off mouse hover during wave state
if (waveState){ return; }
// undo last hover
if (lastHovered>=0){ tiles[lastHovered].y -= tileRise; }
// check for mouse hover
Vector3 onGrid = Vector3Transform({mousePos.x, mousePos.y, 1}, toGrid);
float x_pos = onGrid.x/scale;
float y_pos = onGrid.y/scale;
// make sure mouse within grid boundaries
if ( x_pos >= 0.0f && x_pos < width && y_pos >= 0.0f && y_pos < height){
lastHovered = static_cast<int>(x_pos) + static_cast<int>(y_pos)*width;
tiles[lastHovered].y += tileRise;
}
else {lastHovered = -1;}
// debug info
if (debug){
DrawText(TextFormat("Mouse Position: %i, %i", static_cast<int>(mousePos.x), static_cast<int>(mousePos.y)), 0,0,20, BLACK);
DrawText(TextFormat("Mouse Coord: %i, %i", static_cast<int>(onGrid.x/scale), static_cast<int>(onGrid.y/scale)), 0,25,20, BLACK);
DrawText(TextFormat("Index: %i", lastHovered), 0,50,20, BLACK);
}
}
void Grid::getKeyboardInput(){
if (waveState){
if (IsKeyDown(KEY_UP )){ amplitude++; }
if (IsKeyDown(KEY_DOWN )){ amplitude--; }
if (IsKeyDown(KEY_LEFT )){ wavelength++; }
if (IsKeyDown(KEY_RIGHT )){ wavelength--; }
if (IsKeyPressed(KEY_PERIOD)){ speed++; }
if (IsKeyPressed(KEY_COMMA )){ speed--; }
dt += speed;
}
}
void Grid::draw(const Vector2& mousePos){
// raise tile on mouse hover
mouseHover(mousePos);
getKeyboardInput();
// draw tiles
for (const auto& tile : tiles){
DrawTexturePro(texture, source, {tile.x, tile.y+waveOffset(tile), tile.width, tile.height}, {}, 0.0f, WHITE);
}
// wave state info
if (debug && waveState){
DrawText(TextFormat("Wave Amplitude: %i", amplitude), 0,0,20, BLACK);
DrawText(TextFormat("Wave length: %01.04f", wavelength), 0,25,20, BLACK);
DrawText(TextFormat("Wave Speed: %i", speed), 0,50,20, BLACK);
}
// check transition to wave
if (transitioning){
transitionFactor += transitionSpeed*transitioning;
if (transitionFactor<=0.0f || transitionFactor >=1.0f){
transitioning = false;
std::round(transitionFactor);
}
}
}
float Grid::waveOffset(const Rectangle& tile){
return transitionFactor>0.0f ? transitionFactor*amplitude*std::sin((dt + tile.x + tile.y)*wavelength/100'000.0f) : 0.0f;
}