gamelogic.d 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. module graphics.gamelogic;
  2. import raylib;
  3. import bindbc.lua;
  4. import variables;
  5. import core.stdc.stdlib;
  6. import core.stdc.time;
  7. import graphics.engine;
  8. import scripts.lua;
  9. import std.stdio;
  10. import std.conv;
  11. import graphics.effects;
  12. import std.string;
  13. import std.math;
  14. import dialogs.dialogbox;
  15. import system.abstraction;
  16. import system.config;
  17. import std.file;
  18. /**
  19. * this module contains game logic, which was removed from engine.d for better readability.
  20. */
  21. void gameInit()
  22. {
  23. circle = LoadTexture("res/misc/circle.png");
  24. dialogBackgroundTex = LoadTexture("res/misc/TEX#win_01d.PNG");
  25. choiceWindowTex = LoadTexture("res/misc/TEX#win_00d.PNG");
  26. texturesLoaded = true;
  27. if (WindowShouldClose()) {
  28. currentGameState = GameState.Exit;
  29. } else {
  30. debugWriteln("Game initializing.");
  31. systemSettings = loadSettingsFromConfigFile();
  32. }
  33. }
  34. void effectsLogic()
  35. {
  36. UpdateMusicStream(music);
  37. if (isCameraMoving) {
  38. float delta = GetFrameTime() * cameraMoveSpeed;
  39. camera.target.x += (cameraTargetX - camera.target.x) * delta;
  40. camera.target.y += (cameraTargetY - camera.target.y) * delta;
  41. camera.zoom += (cameraTargetZoom - camera.zoom) * delta;
  42. if (fabs(camera.target.x - cameraTargetX) < 5.0f &&
  43. fabs(camera.target.y - cameraTargetY) < 5.0f &&
  44. fabs(camera.zoom - cameraTargetZoom) < 0.5f) {
  45. isCameraMoving = false;
  46. }
  47. }
  48. playUIAnimation(framesUI);
  49. }
  50. void backgroundLogic() {
  51. if (backgroundFades.length < backgroundTextures.length) {
  52. backgroundFades.length = backgroundTextures.length;
  53. }
  54. for (int i = 0; i < backgroundTextures.length; i++) {
  55. if (backgroundTextures[i].drawTexture) {
  56. if (backgroundFades[i].alpha < 1.0f) {
  57. backgroundFades[i].alpha += GetFrameTime() * backgroundFades[i].fadeSpeed;
  58. if (backgroundFades[i].alpha > 1.0f) backgroundFades[i].alpha = 1.0f;
  59. }
  60. } else {
  61. if (backgroundFades[i].alpha > 0.0f) {
  62. backgroundFades[i].alpha -= GetFrameTime() * backgroundFades[i].fadeSpeed;
  63. if (backgroundFades[i].alpha < 0.0f) backgroundFades[i].alpha = 0.0f;
  64. }
  65. }
  66. if (backgroundFades[i].alpha > 0.0f) {
  67. float centeredX = backgroundTextures[i].x - (backgroundTextures[i].width * backgroundTextures[i].scale / 2);
  68. float centeredY = backgroundTextures[i].y - (backgroundTextures[i].height * backgroundTextures[i].scale / 2);
  69. Color tint = Colors.WHITE;
  70. tint.a = cast(ubyte)(255 * backgroundFades[i].alpha);
  71. DrawTextureEx(backgroundTextures[i].texture,
  72. Vector2(centeredX, centeredY),
  73. 0.0,
  74. backgroundTextures[i].scale,
  75. tint
  76. );
  77. }
  78. }
  79. }
  80. void characterLogic() {
  81. if (characterFades.length < characterTextures.length) {
  82. characterFades.length = characterTextures.length;
  83. }
  84. for (int i = 0; i < characterTextures.length; i++) {
  85. if (characterTextures[i].drawTexture) {
  86. if (characterTextures[i].justDrawn) {
  87. characterFades[i].alpha = 0.0f;
  88. characterTextures[i].justDrawn = false;
  89. }
  90. if (characterFades[i].alpha < 1.0f) {
  91. characterFades[i].alpha += GetFrameTime() * characterFades[i].fadeSpeed;
  92. if (characterFades[i].alpha > 1.0f) characterFades[i].alpha = 1.0f;
  93. }
  94. } else {
  95. if (characterFades[i].alpha > 0.0f) {
  96. characterFades[i].alpha -= GetFrameTime() * characterFades[i].fadeSpeed;
  97. if (characterFades[i].alpha < 0.0f) {
  98. characterFades[i].alpha = 0.0f;
  99. }
  100. }
  101. }
  102. if (characterFades[i].alpha > 0.0f) {
  103. float centeredX = characterTextures[i].x - (characterTextures[i].width * characterTextures[i].scale / 2);
  104. float centeredY = characterTextures[i].y - (characterTextures[i].height * characterTextures[i].scale / 2);
  105. Color tint = Colors.WHITE;
  106. tint.a = cast(ubyte)(255 * characterFades[i].alpha);
  107. DrawTextureEx(characterTextures[i].texture,
  108. Vector2(centeredX, centeredY),
  109. 0.0,
  110. characterTextures[i].scale,
  111. tint);
  112. }
  113. }
  114. }
  115. void dialogLogic() {
  116. if (showDialog) {
  117. displayDialog(messageGlobal, choices, selectedChoice, choicePage, textFont, &showDialog, typingSpeed,
  118. dialogColor, circle, dialogBackgroundTex, choiceWindowTex);
  119. }
  120. }