Pārlūkot izejas kodu

improved code, removed repeating code

quantumde1 2 mēneši atpakaļ
vecāks
revīzija
fe7fd575eb
4 mainītis faili ar 50 papildinājumiem un 117 dzēšanām
  1. 4 7
      source/dialogs/dialogbox.d
  2. 34 71
      source/graphics/gamelogic.d
  3. 8 22
      source/scripts/lua.d
  4. 4 17
      source/variables.d

+ 4 - 7
source/dialogs/dialogbox.d

@@ -123,11 +123,11 @@ void displayDialog(string[] pages, string[] choices, ref int selectedChoice,
             nameRect.y + (nameRect.height - nameSize.y) / 2
         );
         
-        DrawTextEx(dialogFont, name.toStringz(), namePos + Vector2(2, 2), fontSize, spacing, Colors.BLACK);
+        DrawTextEx(dialogFont, name.toStringz(), namePos + Vector2(3, 3), fontSize, spacing, Colors.BLACK);
         DrawTextEx(dialogFont, name.toStringz(), namePos, fontSize, spacing, Colors.WHITE);
     }
 
-    if (IsKeyPressed(KeyboardKey.KEY_ENTER)) {
+    if (IsKeyPressed(KeyboardKey.KEY_ENTER) || IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT)) {
         if (!textFullyDisplayed) {
             textDisplayProgress = displayText.length;
             textFullyDisplayed = true;
@@ -202,7 +202,7 @@ void displayDialog(string[] pages, string[] choices, ref int selectedChoice,
     }
     
     // Choice selection logic
-    if (currentPage == choicePage) {
+    if (textFullyDisplayed && (currentPage == choicePage)) {
         const choiceHeight = 50;
         const choiceSpacing = 10;
         const verticalPadding = 30;
@@ -230,10 +230,6 @@ void displayDialog(string[] pages, string[] choices, ref int selectedChoice,
             bool hovered = CheckCollisionPointRec(mousePos, choiceRect);
             if (hovered) selectedChoice = cast(int)i;
             
-            if (hovered || i == selectedChoice) {
-                DrawRectangleRec(choiceRect, Color(60, 60, 60, 200));
-            }
-            
             Vector2 textSize = MeasureTextEx(dialogFont, choice.toStringz(), 39, spacing);
             Vector2 textPos = Vector2(
                 choiceRect.x + (choiceRect.width - textSize.x)/2,
@@ -258,6 +254,7 @@ void displayDialog(string[] pages, string[] choices, ref int selectedChoice,
     // Fast-forward with Ctrl
     if ((IsKeyDown(KeyboardKey.KEY_LEFT_CONTROL) || IsKeyDown(KeyboardKey.KEY_RIGHT_CONTROL)) 
         && currentPage != choicePage && currentPage < pages.length) {
+        textFullyDisplayed = true;
         currentPage++;
     }
     

+ 34 - 71
source/graphics/gamelogic.d

@@ -19,7 +19,7 @@ import std.file;
 
 /** 
  * this module contains game logic, which was removed from engine.d for better readability.
- */
+**/
  
 void gameInit()
 {
@@ -34,6 +34,37 @@ void gameInit()
     }
 }
 
+void texturesLogic(TextureEngine[] textures) {
+    for (int i = 0; i < textures.length; i++) {
+        if (textures[i].drawTexture) {
+            if (textures[i].alpha < 1.0f) {
+                textures[i].alpha += GetFrameTime() * textures[i].fadeSpeed;
+                if (textures[i].alpha > 1.0f) textures[i].alpha = 1.0f;
+            }
+        } else {
+            if (textures[i].alpha > 0.0f) {
+                textures[i].alpha -= GetFrameTime() * textures[i].fadeSpeed;
+                if (textures[i].alpha < 0.0f) textures[i].alpha = 0.0f;
+            }
+        }
+
+        if (textures[i].alpha > 0.0f) {
+            float centeredX = textures[i].x - (textures[i].width * textures[i].scale / 2);
+            float centeredY = textures[i].y - (textures[i].height * textures[i].scale / 2);
+            
+            Color tint = Colors.WHITE;
+            tint.a = cast(ubyte)(255 * textures[i].alpha);
+            
+            DrawTextureEx(textures[i].texture,
+                Vector2(centeredX, centeredY),
+                0.0,
+                textures[i].scale,
+                tint
+            );
+        }
+    }
+}
+
 void effectsLogic()
 {
     UpdateMusicStream(music);
@@ -53,79 +84,11 @@ void effectsLogic()
 }
 
 void backgroundLogic() {
-    if (backgroundFades.length < backgroundTextures.length) {
-        backgroundFades.length = backgroundTextures.length;
-    }
-
-    for (int i = 0; i < backgroundTextures.length; i++) {
-        if (backgroundTextures[i].drawTexture) {
-            if (backgroundFades[i].alpha < 1.0f) {
-                backgroundFades[i].alpha += GetFrameTime() * backgroundFades[i].fadeSpeed;
-                if (backgroundFades[i].alpha > 1.0f) backgroundFades[i].alpha = 1.0f;
-            }
-        } else {
-            if (backgroundFades[i].alpha > 0.0f) {
-                backgroundFades[i].alpha -= GetFrameTime() * backgroundFades[i].fadeSpeed;
-                if (backgroundFades[i].alpha < 0.0f) backgroundFades[i].alpha = 0.0f;
-            }
-        }
-
-        if (backgroundFades[i].alpha > 0.0f) {
-            float centeredX = backgroundTextures[i].x - (backgroundTextures[i].width * backgroundTextures[i].scale / 2);
-            float centeredY = backgroundTextures[i].y - (backgroundTextures[i].height * backgroundTextures[i].scale / 2);
-            
-            Color tint = Colors.WHITE;
-            tint.a = cast(ubyte)(255 * backgroundFades[i].alpha);
-            
-            DrawTextureEx(backgroundTextures[i].texture,
-                Vector2(centeredX, centeredY),
-                0.0,
-                backgroundTextures[i].scale,
-                tint
-            );
-        }
-    }
+    texturesLogic(backgroundTextures);
 }
 
 void characterLogic() {
-    if (characterFades.length < characterTextures.length) {
-        characterFades.length = characterTextures.length;
-    }
-
-    for (int i = 0; i < characterTextures.length; i++) {
-        if (characterTextures[i].drawTexture) {
-            if (characterTextures[i].justDrawn) {
-                characterFades[i].alpha = 0.0f;
-                characterTextures[i].justDrawn = false;
-            }
-            
-            if (characterFades[i].alpha < 1.0f) {
-                characterFades[i].alpha += GetFrameTime() * characterFades[i].fadeSpeed;
-                if (characterFades[i].alpha > 1.0f) characterFades[i].alpha = 1.0f;
-            }
-        } else {
-            if (characterFades[i].alpha > 0.0f) {
-                characterFades[i].alpha -= GetFrameTime() * characterFades[i].fadeSpeed;
-                if (characterFades[i].alpha < 0.0f) {
-                    characterFades[i].alpha = 0.0f;
-                }
-            }
-        }
-
-        if (characterFades[i].alpha > 0.0f) {
-            float centeredX = characterTextures[i].x - (characterTextures[i].width * characterTextures[i].scale / 2);
-            float centeredY = characterTextures[i].y - (characterTextures[i].height * characterTextures[i].scale / 2);
-            
-            
-            characterColor.a = cast(ubyte)(255 * characterFades[i].alpha);
-            
-            DrawTextureEx(characterTextures[i].texture,
-                        Vector2(centeredX, centeredY), 
-                        0.0, 
-                        characterTextures[i].scale, 
-                        characterColor);
-        }
-    }
+    texturesLogic(characterTextures);
 }
 
 void dialogLogic() {

+ 8 - 22
source/scripts/lua.d

@@ -102,6 +102,9 @@ extern (C) nothrow int luaL_draw2Dbackground(lua_State* L)
         int count = cast(int)luaL_checkinteger(L, 4);
         debugWriteln(backgroundTextures[count]);
         backgroundTextures[count].height = backgroundTextures[count].texture.height;
+        if (backgroundTextures.length < backgroundTextures.length) {
+            backgroundTextures.length = backgroundTextures.length;
+        }
         backgroundTextures[count].width = backgroundTextures[count].texture.width;
         backgroundTextures[count].x = luaL_checknumber(L, 1);
         backgroundTextures[count].y = luaL_checknumber(L, 2);
@@ -151,11 +154,15 @@ extern (C) nothrow int luaL_unload2Dbackground(lua_State* L)
 extern (C) nothrow int luaL_load2Dcharacter(lua_State *L) {
     try
     {
+        
         int count = cast(int) luaL_checkinteger(L, 2);
 
         if (count >= characterTextures.length) {
             characterTextures.length = count + 1;
         }
+        if (characterTextures.length < characterTextures.length) {
+            characterTextures.length = characterTextures.length;
+        }
         if (count < characterTextures.length && characterTextures[count].texture.id != 0) {
             UnloadTexture(characterTextures[count].texture);
         }
@@ -384,24 +391,6 @@ extern (C) nothrow int luaL_getScreenHeight(lua_State* L)
     return 1;
 }
 
-extern (C) nothrow int luaL_getUsedLanguage(lua_State* L)
-{
-    lua_pushstring(L, usedLang.toStringz());
-    return 1;
-}
-
-extern (C) nothrow int luaL_2dModeEnable(lua_State* L)
-{
-    neededDraw2D = true;
-    return 0;
-}
-
-extern (C) nothrow int luaL_2dModeDisable(lua_State* L)
-{
-    neededDraw2D = false;
-    return 0;
-}
-
 extern (C) nothrow int luaL_unloadFont(lua_State *L) {
     UnloadFont(textFont);
     return 0;
@@ -540,7 +529,7 @@ extern (C) nothrow int luaL_loadTexture(lua_State *L) {
         lua_pushcfunction(L, &luaL_textureGC);
         lua_setfield(L, -2, "__gc");
     }
-    
+    SetTextureFilter(texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
     lua_setmetatable(L, -2);
     return 1;
 }
@@ -689,8 +678,6 @@ extern (C) nothrow void luaL_loader(lua_State* L)
     lua_register(L, "unloadMusic", &luaL_unloadMusic);
     lua_register(L, "playSfx", &luaL_playSfx);
     lua_register(L, "stopSfx", &luaL_stopSfx);
-    lua_register(L, "Begin2D", &luaL_2dModeEnable);
-    lua_register(L, "End2D", &luaL_2dModeDisable);
     lua_register(L, "loadCharacter", &luaL_load2Dcharacter);
     lua_register(L, "drawCharacter", &luaL_draw2Dcharacter);
     lua_register(L, "stopDrawCharacter", &luaL_stopDraw2Dcharacter);
@@ -708,7 +695,6 @@ extern (C) nothrow void luaL_loader(lua_State* L)
     lua_register(L, "getScreenWidth", &luaL_getScreenWidth);
     lua_register(L, "isKeyPressed", &luaL_isKeyPressed);
     lua_register(L, "isMouseButtonPressed", &luaL_isMouseButtonPressed);
-    lua_register(L, "getLanguage", &luaL_getUsedLanguage);
     lua_register(L, "setGameState", &luaL_setGameState);
     //raylib direct bindings
     lua_register(L, "loadTexture", &luaL_loadTexture);

+ 4 - 17
source/variables.d

@@ -27,13 +27,6 @@ nothrow void resetAllScriptValues() {
 
 /* system */
 
-struct FadeEffect {
-    float alpha = 0.0f;
-    float targetAlpha = 0.0f;
-    float fadeSpeed = 9.0f;
-    bool isFading = false;
-}
-
 struct SystemSettings {
     int sound_state;
     char right_button;
@@ -54,6 +47,10 @@ struct TextureEngine {
     float y;
     Texture2D texture;
     float scale;
+    float alpha = 0.0f;
+    float targetAlpha = 0.0f;
+    float fadeSpeed = 9.0f;
+    bool isFading = false;
 }
 
 enum GameState {
@@ -84,12 +81,6 @@ Font textFont;
 
 Music music;
 
-FadeEffect[] characterFades;
-
-FadeEffect[] backgroundFades;
-
-FadeEffect dialogFade;
-
 Color characterColor = Color(255, 255, 255);
 
 /* booleans */
@@ -106,8 +97,6 @@ bool luaReload = true;
 
 bool videoFinished = false;
 
-bool neededDraw2D = false;
-
 bool isCameraMoving = false;
 
 bool neededCharacterDrawing = false;
@@ -129,8 +118,6 @@ string[] backlogText;
 
 string luaExec;
 
-string usedLang = "english";
-
 char* musicPath;