raylib/games/transmission/screens/screen_gameplay.c

413 lines
13 KiB
C
Raw Normal View History

/**********************************************************************************************
*
* raylib - Advance Game template
*
* Gameplay Screen Functions Definitions (Init, Update, Draw, Unload)
*
* Copyright (c) 2014-2018 Ramon Santamaria (@raysan5)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
#include "raylib.h"
#include "screens.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//#define MAX_CODING_WORDS 12
//#define MAX_MISSION_WORDS 8
#define MAX_LINE_CHAR 30
/*
2018-12-24 19:46:23 +03:00
// NOTE: Coding words are generic and the same words
// are used for all missions,
typedef enum CodingWords {
POLLO = 0,
CONEJO,
HUEVO,
NIDO,
AIRE,
ARMARIO,
AGUJERO,
COSA,
WORD,
} CodingWords;
*/
2018-12-24 19:46:23 +03:00
static char *codingWords[MAX_CODING_WORDS] = {
"pollo\0",
"conejo\0",
"huevo\0",
2018-12-24 19:46:23 +03:00
"nido\0",
"aire\0",
"armario\0",
"agujero\0",
"platano\0",
"pastel\0",
"mercado\0",
"raton\0",
"melon\0"
};
// Words to be coded or coding words
/*typedef struct Word {
int id;
Rectangle rec;
Rectangle iniRec;
bool hover;
bool picked;
char text[32]; // text
} Word;*/
/*
// Mission information
typedef struct Mission {
int id;
char brief[512]; // Mission briefing
char key[32]; // Mission keyword
char msg[256]; // Message to be coded
int wordsCount; // Number of words to coded
int sols[8]; // Solution code, depends on wordsCount
} Mission;
*/
//----------------------------------------------------------------------------------
// Global Variables Definition (local to this module)
//----------------------------------------------------------------------------------
// Gameplay screen global variables
static int framesCounter;
static int finishScreen;
static Texture2D texBackground;
static Font fontMessage;
static Texture2D texWordsAtlas;
static Texture2D texVignette;
static Sound fxGrab;
static Sound fxPlace;
static Sound fxLeave;
static Music musSpy;
static Word words[MAX_CODING_WORDS] = { 0 };
// Hay que hacerlo global, para poder consultar el resultado desde la endingscreen
//static Word messageWords[MAX_MISSION_WORDS] = { 0 };
static Mission *missions = NULL;
static bool canSend = false;
Vector2 msgOffset = { 430, 300 };
//----------------------------------------------------------------------------------
// Gameplay Screen Functions Definition
//----------------------------------------------------------------------------------
// Gameplay Screen Initialization logic
void InitGameplayScreen(void)
{
framesCounter = 0;
finishScreen = 0;
2018-12-24 19:46:23 +03:00
fontMessage = LoadFontEx("resources/fonts/traveling_typewriter.ttf", 30, 0, 250);
2018-12-24 19:46:23 +03:00
texBackground = LoadTexture("resources/textures/message_background.png");
texVignette = LoadTexture("resources/textures/message_vignette.png");
2018-12-24 19:46:23 +03:00
fxGrab = LoadSound("resources/audio/fx_grab.ogg");
fxPlace = LoadSound("resources/audio/fx_place.ogg");
fxLeave = LoadSound("resources/audio/fx_leave.ogg");
2018-12-24 19:46:23 +03:00
musSpy = LoadMusicStream("resources/audio/s_p_y.xm");
PlayMusicStream(musSpy);
2018-12-24 19:46:23 +03:00
#if defined(PLATFORM_WEB)
#define WORD_ATLAS_FROM_FILE
#endif
#if defined(WORD_ATLAS_FROM_FILE)
texWordsAtlas = LoadTexture("resources/textures/mission_words.png");
#else
// Generate coding words atlas directly from text
Image imWordsBase = LoadImage("resources/textures/words_base.png");
Image imWords = GenImageColor(imWordsBase.width, imWordsBase.height*MAX_CODING_WORDS, WHITE);
2018-12-24 19:46:23 +03:00
for (int i = 0; i < MAX_CODING_WORDS; i++)
{
2018-12-24 19:46:23 +03:00
ImageDraw(&imWords, imWordsBase,
(Rectangle){ 0, 0, imWordsBase.width, imWordsBase.height },
(Rectangle){ 0, imWordsBase.height*i, imWordsBase.width, imWordsBase.height });
2018-12-24 19:46:23 +03:00
ImageDrawTextEx(&imWords,(Vector2){ imWordsBase.width/2 - MeasureTextEx(fontMessage, codingWords[i],
fontMessage.baseSize, 0).x/2, imWordsBase.height*i }, fontMessage, codingWords[i],
fontMessage.baseSize, 0, BLACK);
}
2018-12-24 19:46:23 +03:00
texWordsAtlas = LoadTextureFromImage(imWords);
2018-12-24 19:46:23 +03:00
UnloadImage(imWordsBase);
UnloadImage(imWords);
#endif
// Initialize missions
// WARNING: Some problem with imWords image generation (memory leak?) could cause
// that loading missions before/after generation breaks game, on web is the other way round... :(
missions = LoadMissions("resources/missions.txt");
TraceLog(LOG_WARNING, "Words count %i", missions[currentMission].wordsCount);
// Initialize coding words
for (int i = 0; i < MAX_CODING_WORDS; i++)
{
words[i].id = -1; // Not placed anywhere
words[i].rec.x = 110 + 940*(i/(MAX_CODING_WORDS/2));
words[i].rec.y = 200 + 60*(i%(MAX_CODING_WORDS/2));
words[i].rec.width = 140; // texWordsAtlas.width/MAX_MISSIONS
words[i].rec.height = 35; // texWordsAtlas.height/MAX_MISSION_WORDS
words[i].iniRec = words[i].rec;
words[i].hover = false; // Mouse hover detected
words[i].picked = false; // Mouse picked
2018-12-24 19:46:23 +03:00
//words[i].text = ''; //codingWords[i]; // Fill text if required...
}
2018-12-24 19:46:23 +03:00
// Analize missions[currentMission].msg string for words!
int msgLen = strlen(missions[currentMission].msg);
2018-12-24 19:46:23 +03:00
// Add '/' each MAX_LINE_CHAR chars
int currentLine = 1;
int i = currentLine * MAX_LINE_CHAR;
while (i < msgLen - 1)
{
if (missions[currentMission].msg[i] == ' ')
{
missions[currentMission].msg[i] = '/';
currentLine++;
i = currentLine*MAX_LINE_CHAR;
}
else i++;
2018-12-24 19:46:23 +03:00
}
int currentWord = 0;
int offsetX = 0;
int offsetY = 0;
bool foundWord = false;
int wordInitPosX = 0;
int wordInitPosY = 0;
for (int i = 0; i < msgLen; i++)
2018-12-24 19:46:23 +03:00
{
char c = missions[currentMission].msg[i];
if (foundWord && (c == ' ' || c == '.'))
{
foundWord = false;
2018-12-24 19:46:23 +03:00
messageWords[currentWord - 1].rec.width = (int)MeasureTextEx(fontMessage, SubText(missions[currentMission].msg, wordInitPosX, (i - wordInitPosX)), 30, 0).x;
messageWords[currentWord - 1].rec.height = fontMessage.baseSize;
2018-12-24 19:46:23 +03:00
strncpy(messageWords[currentWord - 1].text, SubText(missions[currentMission].msg, wordInitPosX, (i - wordInitPosX)), i - wordInitPosX);
}
2018-12-24 19:46:23 +03:00
if (c == '@') // One word to change
{
foundWord = true;
missions[currentMission].msg[i] = ' ';
offsetX = (int)MeasureTextEx(fontMessage, SubText(missions[currentMission].msg, wordInitPosY, (i + 1) - wordInitPosY), 30, 0).x;
2018-12-24 19:46:23 +03:00
messageWords[currentWord].rec.x = offsetX;
messageWords[currentWord].rec.y = offsetY;
wordInitPosX = i + 1;
currentWord++;
}
else if (c == '/')
{
missions[currentMission].msg[i] = '\n';
wordInitPosY = i;
offsetY += (fontMessage.baseSize + fontMessage.baseSize/2); // raylib internal increment on line break...
}
}
for (int i = 0; i < missions[currentMission].wordsCount; i++)
{
messageWords[i].id = -1; // Not required for message words, id is the array position
2018-12-24 19:46:23 +03:00
// Recalculate words rectangles considering text offset on screen
messageWords[i].rec.x += msgOffset.x;
messageWords[i].rec.y += msgOffset.y;
2018-12-24 19:46:23 +03:00
// Recalculate words rectangle considering new width height
messageWords[i].rec.x -= (texWordsAtlas.width - messageWords[i].rec.width)/2;
messageWords[i].rec.y -= ((texWordsAtlas.height / MAX_CODING_WORDS) - messageWords[i].rec.height)/2;
2018-12-24 19:46:23 +03:00
//Recalculate width height
messageWords[i].rec.width = texWordsAtlas.width;
messageWords[i].rec.height = texWordsAtlas.height / MAX_CODING_WORDS;
messageWords[i].hover = false; // Mouse hover detected
messageWords[i].picked = false; // Mouse picked
}
}
// Gameplay Screen Update logic
void UpdateGameplayScreen(void)
{
UpdateMusicStream(musSpy);
2018-12-24 19:46:23 +03:00
for (int i = 0; i < MAX_CODING_WORDS; i++)
{
if (CheckCollisionPointRec(GetMousePosition(), words[i].rec))
{
words[i].hover = true;
2018-12-24 19:46:23 +03:00
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
words[i].picked = true;
PlaySound(fxGrab);
}
}
else words[i].hover = false;
2018-12-24 19:46:23 +03:00
if (words[i].picked)
2018-12-24 19:46:23 +03:00
{
for (int j = 0; j < missions[currentMission].wordsCount; j++)
2018-12-24 19:46:23 +03:00
{
if (CheckCollisionPointRec(GetMousePosition(), messageWords[j].rec)) messageWords[j].hover = true;
else messageWords[j].hover = false;
}
2018-12-24 19:46:23 +03:00
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
2018-12-24 19:46:23 +03:00
{
words[i].picked = false;
2018-12-24 19:46:23 +03:00
for (int j = 0; j < missions[currentMission].wordsCount; j++)
2018-12-24 19:46:23 +03:00
{
messageWords[j].hover = false;
2018-12-24 19:46:23 +03:00
if (CheckCollisionPointRec(GetMousePosition(), messageWords[j].rec))
{
PlaySound(fxPlace);
2018-12-24 19:46:23 +03:00
words[i].rec.x = messageWords[j].rec.x;
words[i].rec.y = messageWords[j].rec.y;
2018-12-24 19:46:23 +03:00
if (messageWords[j].id != -1)
{
int id = messageWords[j].id;
words[id].rec = words[id].iniRec;
}
2018-12-24 19:46:23 +03:00
messageWords[j].id = i;
for (int k = 0; k < missions[currentMission].wordsCount; k++)
{
2018-12-24 19:46:23 +03:00
if (j != k && messageWords[j].id == messageWords[k].id)
{
messageWords[k].id = -1;
break;
}
}
break;
}
2018-12-24 19:46:23 +03:00
else
{
PlaySound(fxLeave);
2018-12-24 19:46:23 +03:00
words[i].rec = words[i].iniRec;
if (i == messageWords[j].id) messageWords[j].id = -1;
}
2018-12-24 19:46:23 +03:00
}
}
}
2018-12-24 19:46:23 +03:00
// Move word picked with mouse
if (words[i].picked)
{
words[i].rec.x = GetMouseX() - words[i].rec.width/2;
words[i].rec.y = GetMouseY() - words[i].rec.height/2;
}
}
2018-12-24 19:46:23 +03:00
canSend = true;
2018-12-24 19:46:23 +03:00
for (int j = 0; j < missions[currentMission].wordsCount; j++)
{
if (messageWords[j].id == -1)
{
canSend = false;
break;
}
}
2018-12-24 19:46:23 +03:00
if (canSend && (IsKeyPressed(KEY_ENTER) || IsButtonPressed()))
{
finishScreen = true;
2018-12-24 19:46:23 +03:00
}
}
// Gameplay Screen Draw logic
void DrawGameplayScreen(void)
{
DrawTexture(texBackground, 0, 0, WHITE);
2018-12-24 19:46:23 +03:00
DrawTextEx(fontMessage, missions[currentMission].msg, msgOffset, fontMessage.baseSize, 0, BLACK);
2018-12-24 19:46:23 +03:00
for (int i = 0; i < missions[currentMission].wordsCount; i++)
{
Rectangle recLines = messageWords[i].rec;
DrawRectangleLines(recLines.x, recLines.y, recLines.width, recLines.height, Fade(RED, 0.35f));
2018-12-24 19:46:23 +03:00
if (messageWords[i].hover) DrawRectangleRec(messageWords[i].rec, Fade(RED, 0.30f));
DrawText(FormatText("%i", messageWords[i].id), i*25, 0, 30, RED);
}
for (int i = 0; i < MAX_CODING_WORDS; i++)
{
if (words[i].picked) DrawTextureRec(texWordsAtlas, (Rectangle){ 0, i*35, 140, 35 }, (Vector2){ words[i].rec.x, words[i].rec.y }, MAROON);
else if (words[i].hover) DrawTextureRec(texWordsAtlas, (Rectangle){ 0, i*35, 140, 35 }, (Vector2){ words[i].rec.x, words[i].rec.y }, RED);
else DrawTextureRec(texWordsAtlas, (Rectangle){ 0, i*35, 140, 35 }, (Vector2){ words[i].rec.x, words[i].rec.y }, WHITE);
}
2018-12-24 19:46:23 +03:00
DrawTexturePro(texVignette, (Rectangle){0,0,texVignette.width, texVignette.height}, (Rectangle){0,0,GetScreenWidth(), GetScreenHeight()}, (Vector2){0,0}, 0, WHITE);
2018-12-24 19:46:23 +03:00
if (canSend) DrawButton("enviar");
}
// Gameplay Screen Unload logic
void UnloadGameplayScreen(void)
{
UnloadTexture(texBackground);
UnloadTexture(texVignette);
UnloadTexture(texWordsAtlas);
2018-12-24 19:46:23 +03:00
UnloadSound(fxGrab);
UnloadSound(fxLeave);
UnloadSound(fxPlace);
2018-12-24 19:46:23 +03:00
UnloadMusicStream(musSpy);
2018-12-24 19:46:23 +03:00
free(missions);
}
// Gameplay Screen should finish?
int FinishGameplayScreen(void)
{
return finishScreen;
}