Game.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include "Game.h"
  2. #include "Engine.h"
  3. #include "data/List.h"
  4. #include "input/Controller.h"
  5. struct Line final {
  6. Vector2 a;
  7. Vector2 b;
  8. Color4 color;
  9. float friction;
  10. Line(const Vector2& a, const Vector2& b, Color4 color, float friction)
  11. : a(a), b(b), color(color), friction(friction) {
  12. }
  13. Line(const Vector2& a, const Vector2& b)
  14. : a(a), b(b), color(Color4(0xFF, 0xFF, 0xFF, 0xFF)), friction(0.0f) {
  15. }
  16. };
  17. static List<Line> lines;
  18. static constexpr float SPEED = 2.5f;
  19. static constexpr float GRAVITY = 0.5f;
  20. static constexpr float COLLISION_STEP = 0.005f;
  21. static Vector2 windowSize;
  22. static Vector2 lastPosition;
  23. static Vector2 position{150.0f, 50.0f};
  24. static Vector2 size{50.0f, 50.0f};
  25. static Vector2 velocity;
  26. static Vector2 acceleration;
  27. static Vector2 drag;
  28. static float mass = 5.0f;
  29. static bool onGround = false;
  30. static float jumpPower = 0.0f;
  31. static float steepness = 0.0f;
  32. static float friction = 0.0f;
  33. static bool physicsToggle = true;
  34. static Vector2 waterPosition;
  35. static Vector2 waterSize;
  36. static Vector2 projectileLastPosition;
  37. static Vector2 projectilePosition;
  38. static Vector2 projectileVelocity;
  39. static Vector2 projectileSize{20.0f, 20.0f};
  40. static float lastAngle;
  41. static float angle;
  42. static float angleVelocity = 5.0f;
  43. static void addForce(const Vector2& force) {
  44. acceleration += force / mass;
  45. }
  46. static void addMovement() {
  47. physicsToggle =
  48. physicsToggle ^ Window::Controls::wasReleased(Controller::start);
  49. float movement = Window::Controls::isDown(Controller::right) -
  50. Window::Controls::isDown(Controller::left);
  51. float actualSpeed = SPEED * (1.0f - std::abs(steepness) * onGround);
  52. if(physicsToggle) {
  53. addForce(Vector2(actualSpeed, 0.0f) * movement);
  54. } else {
  55. // movement by velocity should have the same terminal velocity as
  56. // movement by force
  57. float terminalFactor = 1.0f / ((1.0 - drag[0]) * drag[0]);
  58. velocity[0] = (actualSpeed / mass) * terminalFactor * movement;
  59. }
  60. }
  61. static bool isIn(const Vector2& posA, const Vector2& sizeA, const Vector2& posB,
  62. const Vector2& sizeB) {
  63. Vector2 maxA = posA + sizeA;
  64. Vector2 maxB = posB + sizeB;
  65. return posA[0] < maxB[0] && maxA[0] > posB[0] && posA[1] < maxB[1] &&
  66. maxA[1] > posB[1];
  67. }
  68. float ERROR = 1.0f / 512.0f;
  69. static bool isBetween(float y, float y1, float y2) {
  70. return y >= std::min(y1, y2) && y <= std::max(y1, y2);
  71. }
  72. static bool compareFloats(float a, float b) {
  73. return std::abs(a - b) < ERROR;
  74. }
  75. static bool intersect(float x11, float y11, float x12, float y12, float x21,
  76. float y21, float x22, float y22) {
  77. if(compareFloats(x11, x12)) {
  78. if(compareFloats(x21, x22)) {
  79. return false;
  80. } else {
  81. if(!isBetween(x11, x21, x22)) {
  82. return false;
  83. }
  84. float k = (y21 - y22) / (x21 - x22);
  85. float d = y22 - k * x22;
  86. float y = d + x11 * k;
  87. return isBetween(y, y11, y12) && isBetween(y, y21, y22);
  88. }
  89. } else {
  90. if(compareFloats(x21, x22)) {
  91. if(!isBetween(x21, x11, x12)) {
  92. return false;
  93. }
  94. float k = (y11 - y12) / (x11 - x12);
  95. float d = y12 - k * x12;
  96. float y = d + x21 * k;
  97. return isBetween(y, y11, y12) && isBetween(y, y21, y22);
  98. } else {
  99. float k1 = (y11 - y12) / (x11 - x12);
  100. float k2 = (y21 - y22) / (x21 - x22);
  101. if(compareFloats(k1, k2)) {
  102. return false;
  103. }
  104. float d1 = y12 - k1 * x12;
  105. float d2 = y22 - k2 * x22;
  106. float x = (d1 - d2) / (k2 - k1);
  107. if(!isBetween(x, x11, x12) || !isBetween(x, x21, x22)) {
  108. return false;
  109. }
  110. float y = k1 * x + d1;
  111. return isBetween(y, y11, y12) && isBetween(y, y21, y22);
  112. }
  113. }
  114. }
  115. static bool areColliding(const Line& a, const Line& b) {
  116. return intersect(a.a[0], a.a[1], a.b[0], a.b[1], b.a[0], b.a[1], b.b[0],
  117. b.b[1]);
  118. }
  119. static bool doesPlayerCollide() {
  120. Vector2 posX = position + Vector2(size[0], 0.0f);
  121. Vector2 posY = position + Vector2(0.0f, size[1]);
  122. Vector2 posXY = position + size;
  123. for(const Line& line : lines) {
  124. if(areColliding(line, {position, posX}) ||
  125. areColliding(line, {position, posY}) ||
  126. areColliding(line, {posX, posXY}) ||
  127. areColliding(line, {posY, posXY})) {
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. static void slopeSteepnessAndFriction() {
  134. Line left{position - Vector2(0.0f, 10.0f * COLLISION_STEP),
  135. position + Vector2(0.0f, size[1])};
  136. Line right{position + Vector2(size[0], -10.0f * COLLISION_STEP),
  137. position + size};
  138. float min = 100.0f;
  139. float value = 100.0f;
  140. for(const Line& line : lines) {
  141. if(areColliding(line, left) || areColliding(line, right)) {
  142. float f = (line.a[1] - line.b[1]) / (line.a[0] - line.b[0]);
  143. if(std::abs(f) < min) {
  144. min = std::abs(f);
  145. value = f;
  146. friction = line.friction;
  147. }
  148. }
  149. }
  150. steepness = value;
  151. }
  152. static void applyDrag() {
  153. waterPosition = windowSize * Vector2(0.5f, 0.0f);
  154. waterSize = windowSize * Vector2(0.5f, 0.125f);
  155. drag = Vector2(0.1f, 0.1f);
  156. if(isIn(position, size, waterPosition, waterSize)) {
  157. drag *= 4.0f;
  158. }
  159. acceleration -= velocity * drag;
  160. }
  161. static void applyGravity() {
  162. acceleration -= Vector2(0.0f, GRAVITY);
  163. }
  164. static void handleJump() {
  165. bool justJumped = false;
  166. if(Window::Controls::isDown(Controller::a) && onGround) {
  167. jumpPower = 15.0f;
  168. onGround = false;
  169. justJumped = true;
  170. }
  171. addForce(Vector2(0.0f, jumpPower));
  172. jumpPower *= 0.9f;
  173. jumpPower *= Window::Controls::isDown(Controller::a) &&
  174. (velocity[1] > 0.0f || justJumped);
  175. }
  176. static void relaunchProjectile() {
  177. if(projectilePosition[1] >= 0.0f &&
  178. !isIn(position, size, projectilePosition, projectileSize)) {
  179. return;
  180. }
  181. projectilePosition = windowSize * Vector2(0.0f, 0.5f);
  182. projectileLastPosition = projectilePosition;
  183. Vector2 start = projectilePosition;
  184. Vector2 end = position + size * 0.5f;
  185. constexpr float strengh = 25.0f;
  186. float best = 100000000.0f;
  187. float bestRad = 0.0f;
  188. for(float angle = -30.0f; angle < 90.0f; angle += 0.25f) {
  189. float rad = angle * 0.017453293f;
  190. Vector2 v(strengh * cosf(rad), strengh * sinf(rad));
  191. float t = (end[0] - start[0]) / v[0];
  192. float y = v[1] * t - GRAVITY * t * t * 0.5f + start[1];
  193. float diff = std::abs(y - end[1]);
  194. if(diff < best) {
  195. bestRad = rad;
  196. best = diff;
  197. }
  198. }
  199. projectileVelocity =
  200. Vector2(strengh * cosf(bestRad), strengh * sinf(bestRad));
  201. }
  202. static float sign(float f) {
  203. return (f > 0.0f) - (f < 0.0f);
  204. }
  205. static void applyFriction() {
  206. if(!onGround) {
  207. return;
  208. }
  209. float rad = atanf(steepness);
  210. float c = cosf(rad);
  211. float fnLength = mass * GRAVITY * c;
  212. Vector2 fn(fnLength * c, fnLength * sinf(rad)); // already swapt
  213. Vector2 fullFriction = fn * friction;
  214. fullFriction =
  215. Vector2(std::abs(fullFriction[0]), std::abs(fullFriction[1]));
  216. float signX = sign(acceleration[0]);
  217. float signY = sign(acceleration[1]);
  218. fullFriction *= -Vector2(signX, signY);
  219. addForce(fullFriction);
  220. if(sign(acceleration[0]) != signX) {
  221. acceleration[0] = 0.0f;
  222. }
  223. if(sign(acceleration[1]) != signY) {
  224. acceleration[1] = 0.0f;
  225. }
  226. }
  227. static void addLines() {
  228. lines.clear();
  229. lines.add(Line({0.0f, 0.0f}, windowSize * Vector2(1.0f, 0.0f)));
  230. lines.add(Line({0.0f, 0.0f}, windowSize * Vector2(0.0f, 1.0f)));
  231. lines.add(Line(windowSize, windowSize * Vector2(1.0f, 0.0f)));
  232. lines.add(windowSize * Vector2(0.75f, 0.0f),
  233. windowSize * Vector2(1.0f, 0.5f));
  234. lines.add(Line({0.0f, 30.0f}, {400.0f, 0.0f}));
  235. lines.add(Line({0.0f, 60.0f}, {300.0f, 0.0f}));
  236. lines.add(Line({0.0f, 90.0f}, {200.0f, 0.0f}));
  237. lines.add(Line({0.0f, 120.0f}, {100.0f, 0.0f}));
  238. lines.add(Line({100.0f, 180.0f}, {200.0f, 180.0f}));
  239. lines.add(Line({200.0f, 180.0f}, {300.0f, 230.0f},
  240. Color4(0xF0, 0x00, 0x00, 0xFF), 0.2f));
  241. lines.add(Line({300.0f, 230.0f}, {400.0f, 280.0f},
  242. Color4(0xC0, 0x00, 0x00, 0xFF), 0.4f));
  243. lines.add(Line({400.0f, 280.0f}, {500.0f, 330.0f},
  244. Color4(0x90, 0x00, 0x00, 0xFF), 0.6f));
  245. lines.add(Line({500.0f, 330.0f}, {600.0f, 380.0f},
  246. Color4(0x60, 0x00, 0x00, 0xFF), 0.8f));
  247. lines.add(Line({300.0f, 140.0f}, {400.0f, 200.0f},
  248. Color4(0xF0, 0x00, 0x00, 0xFF), 0.2f));
  249. lines.add(Line({400.0f, 200.0f}, {500.0f, 220.0f},
  250. Color4(0xC0, 0x00, 0x00, 0xFF), 0.4f));
  251. lines.add(Line({500.0f, 220.0f}, {600.0f, 240.0f},
  252. Color4(0x90, 0x00, 0x00, 0xFF), 0.6f));
  253. lines.add(Line({600.0f, 240.0f}, {700.0f, 260.0f},
  254. Color4(0x60, 0x00, 0x00, 0xFF), 0.8f));
  255. lines.add(Line({600.0f, 380.0f}, {1000.0f, 580.0f}));
  256. }
  257. void Game::tick() {
  258. windowSize = Engine::getSize().toFloat();
  259. lastPosition = position;
  260. acceleration = Vector2();
  261. applyGravity();
  262. addMovement();
  263. handleJump();
  264. applyFriction();
  265. applyDrag();
  266. if(steepness != 100.0f) {
  267. if(steepness < -1.0f) {
  268. acceleration[0] += 0.1f;
  269. } else if(steepness > 1.0f) {
  270. acceleration[0] -= 0.1f;
  271. }
  272. }
  273. addLines();
  274. velocity += acceleration;
  275. onGround = false;
  276. Vector2 energy = velocity;
  277. while(energy[0] != 0.0f || energy[1] != 0.0f) {
  278. for(int i = 0; i < 2; i++) {
  279. if(energy[i] != 0.0f) {
  280. float old = position[i];
  281. if(energy[i] > COLLISION_STEP) {
  282. position[i] += COLLISION_STEP;
  283. energy[i] -= COLLISION_STEP;
  284. } else if(energy[i] < -COLLISION_STEP) {
  285. position[i] -= COLLISION_STEP;
  286. energy[i] += COLLISION_STEP;
  287. } else {
  288. position[i] += energy[i];
  289. energy[i] = 0.0f;
  290. }
  291. if(doesPlayerCollide()) {
  292. if(i == 0) {
  293. float oldY = position[1];
  294. position[1] += COLLISION_STEP;
  295. if(!doesPlayerCollide()) {
  296. continue;
  297. }
  298. position[1] = oldY;
  299. }
  300. energy[i] = 0.0f;
  301. velocity[i] = 0.0f;
  302. position[i] = old;
  303. }
  304. }
  305. }
  306. }
  307. float oldSteepness = steepness;
  308. slopeSteepnessAndFriction();
  309. onGround = std::abs(steepness) < 1.0f;
  310. if(onGround) {
  311. velocity[1] = 0.0f;
  312. }
  313. if(steepness == 100.0f) {
  314. steepness = oldSteepness;
  315. }
  316. projectileLastPosition = projectilePosition;
  317. projectileVelocity -= Vector2(0.0f, GRAVITY);
  318. projectilePosition += projectileVelocity;
  319. relaunchProjectile();
  320. lastAngle = angle;
  321. angle += angleVelocity;
  322. }
  323. void Game::render(float lag, Renderer& r) {
  324. r.translateTo(0.0f, 0.0f)
  325. .scale(1.0f, -1.0f)
  326. .translateY(Engine::getSize()[1])
  327. .update();
  328. r.drawRectangle(waterPosition, waterSize, Color4(0x00, 0x00, 0xFF, 0xFF));
  329. Vector2 pos = Math::interpolate(lastPosition, position, lag);
  330. r.push();
  331. r.translateTo(0.0f, 0.0f)
  332. .translate(-size * 0.5f)
  333. .rotate(atanf(steepness) * 180.0f / M_PI)
  334. .translate(pos)
  335. .translate(size * 0.5f)
  336. .scale(1.0f, -1.0f)
  337. .translateY(Engine::getSize()[1])
  338. .update();
  339. r.drawRectangle(Vector2(), size, Color4(0xFF, 0x00, 0x00, 0xFF));
  340. r.pop();
  341. r.update();
  342. for(int i = 0; i < 360; i += 120) {
  343. r.push();
  344. r.translateTo(0.0f, 0.0f)
  345. .translate(size * 0.75f)
  346. .rotate(Math::interpolate(lastAngle, angle, lag) + i)
  347. .translate(pos + size * 0.5f)
  348. .scale(1.0f, -1.0f)
  349. .translateY(Engine::getSize()[1])
  350. .update();
  351. r.drawRectangle(Vector2(), size * 0.25f,
  352. Color4(0xFF, 0x00, 0xFF, 0xFF));
  353. r.pop();
  354. r.update();
  355. }
  356. r.drawRectangle(
  357. Math::interpolate(projectileLastPosition, projectilePosition, lag),
  358. projectileSize, Color4(0xFF, 0xFF, 0x00, 0xFF));
  359. for(const Line& line : lines) {
  360. Vector2 dir = line.b - line.a;
  361. Vector2 normal(dir[1], -dir[0]);
  362. normal.normalize();
  363. normal *= 2.0f;
  364. Vector2 ap = line.a + normal;
  365. Vector2 bp = line.b + normal;
  366. Vector2 an = line.a - normal;
  367. Vector2 bn = line.b - normal;
  368. r.drawTriangle(ap, bp, an, line.color);
  369. r.drawTriangle(bp, an, bn, line.color);
  370. }
  371. r.translateTo(0.0f, 0.0f).update();
  372. r.setStringSize(2);
  373. float y = 10.0f;
  374. y = r.drawString(10.0f, y,
  375. physicsToggle ? "Force Physics" : "Velocity Physics");
  376. StringBuffer<100> s("a = ");
  377. s.append(acceleration).append(" before collision");
  378. y = r.drawString(10.0f, y, s);
  379. s.clear().append("v = ").append(velocity).append(" after collision");
  380. y = r.drawString(10.0f, y, s);
  381. s.clear().append("Ground = ").append(onGround);
  382. y = r.drawString(10.0f, y, s);
  383. s.clear().append("Angle = ").append(atanf(steepness) * 57.295779513f);
  384. y = r.drawString(10.0f, y, s);
  385. s.clear().append("Friction = ").append(friction);
  386. y = r.drawString(10.0f, y, s);
  387. }
  388. bool Game::isRunning() {
  389. return !Window::Controls::isDown(Controller::select);
  390. }