Added some basic font rendering and texas holdem
This commit is contained in:
34
src/card/card.c
Normal file
34
src/card/card.c
Normal file
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "card.h"
|
||||
|
||||
void cardDeckFill(card_t *deck) {
|
||||
uint8_t i;
|
||||
for(i = 0; i < CARD_DECK_SIZE; i++) {
|
||||
deck[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
void cardShuffle(card_t *hand, uint8_t cardCount) {
|
||||
uint8_t i, j;
|
||||
card_t temporary;
|
||||
for(i = 0; i < cardCount - 1; i++) {
|
||||
// Select random element from remaining elements.
|
||||
j = u8randRange(i, cardCount);
|
||||
temporary = hand[j];// Take out other card
|
||||
hand[j] = hand[i];// Move my card there
|
||||
hand[i] = temporary;// Put other card here.
|
||||
}
|
||||
}
|
||||
|
||||
void cardDeal(card_t *deck, card_t *hand, uint8_t deckSize, uint8_t handSize) {
|
||||
card_t card;
|
||||
card = deck[deckSize-1];
|
||||
deck[deckSize-1] = 0x00;
|
||||
hand[handSize] = card;
|
||||
}
|
35
src/card/card.h
Normal file
35
src/card/card.h
Normal file
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "../util/rand.h"
|
||||
|
||||
/**
|
||||
* Fills a deck with a standard set of cards (unshuffled)
|
||||
*
|
||||
* @param deck Deck to fill. Must be at least 52 elements.
|
||||
*/
|
||||
void cardDeckFill(card_t *deck);
|
||||
|
||||
/**
|
||||
* Shuffles the given hand or deck.
|
||||
*
|
||||
* @param hand The hand/deck to shuffle.
|
||||
* @param cardCount The amount of cards that are in that deck/hand.
|
||||
*/
|
||||
void cardShuffle(card_t *hand, uint8_t cardCount);
|
||||
|
||||
/**
|
||||
* Deals a card of the top of the deck into the given hand.
|
||||
*
|
||||
* @param deck Deck to take from.
|
||||
* @param hand Hand to put into.
|
||||
* @param deckSize Size of the current deck.
|
||||
* @param handSize Size of the current hand.
|
||||
*/
|
||||
void cardDeal(card_t *deck, card_t *hand, uint8_t deckSize, uint8_t handSize);
|
169
src/card/poker/holdem.c
Normal file
169
src/card/poker/holdem.c
Normal file
@ -0,0 +1,169 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "holdem.h"
|
||||
|
||||
void holdemGameInit(holdemgame_t *game) {
|
||||
uint8_t i;
|
||||
holdemplayer_t *player;
|
||||
|
||||
// Set Blinds to nil.
|
||||
game->blindBig = 0;
|
||||
game->blindSmall = 0;
|
||||
|
||||
// Reset the players
|
||||
for(i = 0; i < HOLDEM_PLAYER_COUNT; i++) {
|
||||
player = game->players + i;
|
||||
|
||||
// Set their state to 0
|
||||
player->state = 0x00;
|
||||
|
||||
// Set their chips to zero
|
||||
player->chips = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void holdemRoundInit(holdemgame_t *game) {
|
||||
uint8_t i;
|
||||
holdemplayer_t *player;
|
||||
|
||||
// Refill the deck
|
||||
cardDeckFill(game->deck);
|
||||
game->deckSize = CARD_DECK_SIZE;
|
||||
|
||||
// Reset the pot
|
||||
game->pot = 0;
|
||||
|
||||
// Reset the cards
|
||||
game->cardsFacing = 0;
|
||||
|
||||
// Reset the players
|
||||
for(i = 0; i < HOLDEM_PLAYER_COUNT; i++) {
|
||||
player = game->players + i;
|
||||
|
||||
// Clear Round State(s)
|
||||
player->state &= ~(
|
||||
HOLDEM_STATE_FOLDED |
|
||||
HOLDEM_STATE_SHOWING
|
||||
);
|
||||
|
||||
player->cardCount = 0;
|
||||
|
||||
// Set the players' current bet to zero.
|
||||
player->currentBet = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void holdemDeal(holdemgame_t *game, holdemplayer_t *player) {
|
||||
cardDeal(game->deck, player->cards, game->deckSize, player->cardCount);
|
||||
game->deckSize--;
|
||||
player->cardCount++;
|
||||
}
|
||||
|
||||
void holdemDealAll(holdemgame_t *game, uint8_t count) {
|
||||
uint8_t i, j;
|
||||
for(i = 0; i < count; i++) {
|
||||
for(j = 0; j < HOLDEM_PLAYER_COUNT; j++) {
|
||||
holdemDeal(game, game->players + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void holdemFlop(holdemgame_t *game) {
|
||||
if(game->cardsFacing >= HOLDEM_DEALER_HAND) return;
|
||||
uint8_t i, count;
|
||||
|
||||
|
||||
// Burn the card off the top
|
||||
game->deckSize -= 1;
|
||||
|
||||
// Change count depending on facing
|
||||
count = game->cardsFacing == 0 ? 0x03 : 0x01;
|
||||
|
||||
// Deal
|
||||
for(i = 0; i < count; i++) {
|
||||
cardDeal(game->deck, game->cards, game->deckSize, game->cardsFacing);
|
||||
game->deckSize -= 1;
|
||||
game->cardsFacing += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void holdemRenderInit(holdemrender_t *render) {
|
||||
render->batch = spriteBatchCreate(
|
||||
CARD_DECK_SIZE + (HOLDEM_PLAYER_COUNT * HOLDEM_PLAYER_HAND)
|
||||
);
|
||||
render->texture = assetTextureLoad("cards_normal.png");
|
||||
render->tileset = tilesetCreate(
|
||||
CARD_COUNT_PER_SUIT, 6,
|
||||
render->texture->width, render->texture->height,
|
||||
0, 0, 0, 0
|
||||
);
|
||||
}
|
||||
|
||||
void holdemRenderCard(holdemrender_t *render, card_t card, bool faceUp, float x, float y, float z) {
|
||||
int32_t i;
|
||||
|
||||
if(faceUp) {
|
||||
uint8_t suit = card / CARD_COUNT_PER_SUIT;
|
||||
uint8_t num = card % CARD_COUNT_PER_SUIT;
|
||||
i = num + ((
|
||||
suit == 0 ? 2 :
|
||||
suit == 1 ? 3 :
|
||||
suit == 2 ? 1 :
|
||||
0
|
||||
) * CARD_COUNT_PER_SUIT);
|
||||
} else {
|
||||
i = 4 * render->tileset->columns;
|
||||
}
|
||||
|
||||
tilesetdiv_t *div = render->tileset->divisions + i;
|
||||
|
||||
spriteBatchQuad(render->batch, -1,
|
||||
x, y, z,
|
||||
3, 4,
|
||||
div->x0, div->y0, div->x1, div->y1
|
||||
);
|
||||
}
|
||||
|
||||
void holdemRender(holdemrender_t *render, holdemgame_t *game) {
|
||||
uint8_t i, j;
|
||||
holdemplayer_t *player;
|
||||
|
||||
shaderUseTexture(GAME_STATE.shaderWorld, render->texture);
|
||||
|
||||
// Flush
|
||||
spriteBatchFlush(render->batch);
|
||||
|
||||
// Render Deck
|
||||
for(i = 0; i < game->deckSize; i++) {
|
||||
holdemRenderCard(render, game->deck[i], false, 0, 6, i * 0.05f);
|
||||
}
|
||||
for(i = 0; i < game->cardsFacing; i++) {
|
||||
holdemRenderCard(render, game->cards[i], true, 0, -i*1.5, i * 0.05f);
|
||||
}
|
||||
|
||||
// Render each players's hand
|
||||
for(i = 0; i < HOLDEM_PLAYER_COUNT; i++) {
|
||||
player = game->players + i;
|
||||
|
||||
for(j = 0; j < player->cardCount; j++) {
|
||||
holdemRenderCard(render, player->cards[j], true,
|
||||
4*(i+1), -(j * 1), j * 0.05f
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw
|
||||
spriteBatchDraw(render->batch, 0, -1);
|
||||
}
|
||||
|
||||
uint32_t holdemBet(holdemgame_t *game, holdemplayer_t *player, uint32_t amount) {
|
||||
uint32_t realAmount = mathMin(player->chips, amount);
|
||||
game->pot += realAmount;
|
||||
player->chips -= realAmount;
|
||||
return realAmount;
|
||||
}
|
85
src/card/poker/holdem.h
Normal file
85
src/card/poker/holdem.h
Normal file
@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "../card.h"
|
||||
#include "../../util/rand.h"
|
||||
#include "../../display/spritebatch.h"
|
||||
#include "../../display/texture.h"
|
||||
#include "../../display/tileset.h"
|
||||
#include "../../display/shader.h"
|
||||
#include "../../file/asset.h"
|
||||
|
||||
/**
|
||||
* Initializes a Teax Hold'em Game for the first time.
|
||||
*
|
||||
* @param game Game to initialize.
|
||||
*/
|
||||
void holdemGameInit(holdemgame_t *game);
|
||||
|
||||
/**
|
||||
* Initializes a Texas Hold'em Round for the first time. Does not affect the
|
||||
* global game state.
|
||||
*
|
||||
* @param game Game's round you want to initialize.
|
||||
*/
|
||||
void holdemRoundInit(holdemgame_t *game);
|
||||
|
||||
/**
|
||||
* Deals a card to the given player, does all the follow up.
|
||||
*
|
||||
* @param game Game who's deck to deal from.
|
||||
* @param player Player to deal into.
|
||||
*/
|
||||
void holdemDeal(holdemgame_t *game, holdemplayer_t *player);
|
||||
|
||||
/**
|
||||
* Deal cards to all players.
|
||||
*
|
||||
* @param game Game and players to deal around.
|
||||
* @param count Count of cards to deal to each player.
|
||||
*/
|
||||
void holdemDealAll(holdemgame_t *game, uint8_t count);
|
||||
|
||||
/**
|
||||
* Draw the flop, turn or river
|
||||
*
|
||||
* @param game Game to flop/turn/river.
|
||||
*/
|
||||
void holdemFlop(holdemgame_t *game);
|
||||
|
||||
|
||||
/**
|
||||
* Initialize the Texas Hold'em Renderer.
|
||||
*
|
||||
* @param render Renderer to init.
|
||||
*/
|
||||
void holdemRenderInit(holdemrender_t *render);
|
||||
|
||||
/**
|
||||
* Renders a card.
|
||||
*
|
||||
* @param render Renderer to render against
|
||||
* @param card Card to render.
|
||||
*/
|
||||
void holdemRenderCard(holdemrender_t *render, card_t card, bool faceUp, float x, float y, float z);
|
||||
|
||||
/**
|
||||
* Render an entire holdem game.
|
||||
*/
|
||||
void holdemRender(holdemrender_t *render, holdemgame_t *game);
|
||||
|
||||
/**
|
||||
* Takes the given bet from a player
|
||||
*
|
||||
* @param game Game to add the bet to.
|
||||
* @param player Player to take the bet from.
|
||||
* @param amount Amount to try and take.
|
||||
* @return The real amount that was taken, chips considered.
|
||||
*/
|
||||
uint32_t holdemBet(holdemgame_t *game, holdemplayer_t *player, uint32_t amount);
|
47
src/display/gui/font.c
Normal file
47
src/display/gui/font.c
Normal file
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "font.h"
|
||||
|
||||
tilesetdiv_t * fontGetCharacterDivision(tileset_t *tileset, char character) {
|
||||
int32_t i = ((int32_t)character) - FONT_CHAR_START;
|
||||
return tileset->divisions + i;
|
||||
}
|
||||
|
||||
void fontSpriteBatchBuffer(spritebatch_t *batch, tileset_t *tileset,
|
||||
char *string, float x, float y, float z, float charWidth, float charHeight
|
||||
) {
|
||||
int32_t i;
|
||||
char c;
|
||||
tilesetdiv_t *div;
|
||||
float cx, cy;
|
||||
|
||||
i = 0;
|
||||
cx = x, cy = y;
|
||||
|
||||
while(true) {
|
||||
c = string[i];
|
||||
if(c == '\0') break;
|
||||
i++;
|
||||
|
||||
if(c == '\n') {
|
||||
cx = x;
|
||||
cy += charWidth;
|
||||
continue;
|
||||
} else if(c == ' ') {
|
||||
cx += charHeight;
|
||||
continue;
|
||||
}
|
||||
|
||||
div = fontGetCharacterDivision(tileset, c);
|
||||
spriteBatchQuad(batch, -1,
|
||||
cx, cy, z, charWidth, charHeight,
|
||||
div->x0, div->y0, div->x1, div->y1
|
||||
);
|
||||
cx += charWidth;
|
||||
}
|
||||
}
|
17
src/display/gui/font.h
Normal file
17
src/display/gui/font.h
Normal file
@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "../spritebatch.h"
|
||||
|
||||
#define FONT_CHAR_START 33
|
||||
|
||||
tilesetdiv_t * fontGetCharacterDivision(tileset_t *tileset, char character);
|
||||
void fontSpriteBatchBuffer(spritebatch_t *batch, tileset_t *tileset,
|
||||
char *string, float x, float y, float z, float charWidth, float charHeight
|
||||
);
|
@ -14,7 +14,7 @@ tileset_t * tilesetCreate(
|
||||
int32_t borderX, int32_t borderY
|
||||
) {
|
||||
tileset_t *tileset;
|
||||
float divX, divY, tdivX;
|
||||
float tdivX, tdivY;
|
||||
int32_t x, y, i;
|
||||
|
||||
tileset = malloc(sizeof(tileset_t));
|
||||
@ -31,21 +31,30 @@ tileset_t * tilesetCreate(
|
||||
tileset->rows = rows;
|
||||
|
||||
// Calculate division sizes (pixels)
|
||||
divX = (width - (borderX * 2) - (gapX * (columns - 1))) / columns;
|
||||
divY = (height - (borderY * 2) - (gapY * (rows - 1))) / rows;
|
||||
tileset->divX = (width - (borderX * 2) - (gapX * (columns - 1))) / columns;
|
||||
tileset->divY = (height - (borderY * 2) - (gapY * (rows - 1))) / rows;
|
||||
|
||||
// Calculate the division sizes (units)
|
||||
divX = divX / width;
|
||||
divY = divY / height;
|
||||
tdivX = tileset->divX / width;
|
||||
tdivY = tileset->divY / height;
|
||||
|
||||
// Calculate the divisions
|
||||
// Calculate the divisions (in units)
|
||||
i = -1;
|
||||
for(y = 0; y < rows; y++) {
|
||||
for(x = 0; x < columns; x++) {
|
||||
tileset->divisions[++i].x0 = borderX + (divX * x) + (gapX * x);
|
||||
tileset->divisions[i].y0 = borderY + (divY * y) + (gapY * y);
|
||||
tileset->divisions[i].x1 = tileset->divisions[i].x0 + divX;
|
||||
tileset->divisions[i].y1 = tileset->divisions[i].y0 + divY;
|
||||
tileset->divisions[++i].x0 = (
|
||||
borderX + (tileset->divX * x) + (gapX * x)
|
||||
) / width;
|
||||
tileset->divisions[i].x1 = tileset->divisions[i].x0 + tdivX;
|
||||
|
||||
// tileset->divisions[i].y0 = (borderY + (divY * y) + (gapY * y)) / height;
|
||||
// tileset->divisions[i].y1 = tileset->divisions[i].y0 + tdivY;
|
||||
|
||||
// Vertically flipped for OpenGL
|
||||
tileset->divisions[i].y1 = (
|
||||
borderY + (tileset->divY * y) + (gapY * y)
|
||||
) / height;
|
||||
tileset->divisions[i].y0 = tileset->divisions[i].y1 + tdivY;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,12 @@
|
||||
|
||||
game_t GAME_STATE;
|
||||
|
||||
holdemgame_t holdem;
|
||||
holdemrender_t render;
|
||||
|
||||
texture_t *fontTexture;
|
||||
spritebatch_t *fontBatch;
|
||||
|
||||
bool gameInit() {
|
||||
// Init the game
|
||||
GAME_STATE.name = GAME_NAME;
|
||||
@ -19,13 +25,31 @@ bool gameInit() {
|
||||
inputInit();
|
||||
worldInit();
|
||||
|
||||
holdemGameInit(&holdem);
|
||||
holdemRenderInit(&render);
|
||||
|
||||
holdemRoundInit(&holdem);
|
||||
cardShuffle(holdem.deck, holdem.deckSize);
|
||||
|
||||
// Deal Card
|
||||
holdemDealAll(&holdem, 2);
|
||||
holdemFlop(&holdem);
|
||||
|
||||
fontTexture = assetTextureLoad("font.png");
|
||||
fontBatch = spriteBatchCreate(100);
|
||||
tileset_t *tileset = tilesetCreate(20, 20,
|
||||
fontTexture->width, fontTexture->height,
|
||||
1, 1, 1, 1
|
||||
);
|
||||
|
||||
char *buffer = "!\"#$%";
|
||||
fontSpriteBatchBuffer(fontBatch, tileset, buffer, 0, 0, 0, 1.1, 1.5);
|
||||
|
||||
// Load the world shader.
|
||||
GAME_STATE.shaderWorld = assetShaderLoad(
|
||||
"shaders/test.vert", "shaders/test.frag"
|
||||
);
|
||||
|
||||
entityInit(0x00, 0x01);
|
||||
|
||||
// Init the input manger.
|
||||
return true;
|
||||
}
|
||||
@ -39,16 +63,21 @@ bool gameUpdate(float platformDelta) {
|
||||
shaderUse(GAME_STATE.shaderWorld);
|
||||
|
||||
// Set up the camera.
|
||||
int32_t d = 10;
|
||||
cameraLookAt(&GAME_STATE.cameraWorld, d, d, d, 0, 0, 0);
|
||||
int x = 10;
|
||||
cameraLookAt(&GAME_STATE.cameraWorld, x, 2.5f, 50, x, 2, 0);
|
||||
cameraPerspective(&GAME_STATE.cameraWorld, 45.0f,
|
||||
((float)RENDER_STATE.width) / ((float)RENDER_STATE.height),
|
||||
0.5f, 500.0f
|
||||
);
|
||||
shaderUseCamera(GAME_STATE.shaderWorld, &GAME_STATE.cameraWorld);
|
||||
|
||||
|
||||
|
||||
shaderUseTexture(GAME_STATE.shaderWorld, fontTexture);
|
||||
spriteBatchDraw(fontBatch, 0, -1);
|
||||
|
||||
// holdemRender(&render, &holdem);
|
||||
// Render the game scene.
|
||||
worldRender();
|
||||
// worldRender();
|
||||
|
||||
if(inputIsPressed(INPUT_NULL)) return false;
|
||||
return true;
|
||||
|
@ -14,6 +14,11 @@
|
||||
#include "../world/world.h"
|
||||
#include "../world/entity/entity.h"
|
||||
|
||||
#include "../card/poker/holdem.h"
|
||||
#include "../display/gui/font.h"
|
||||
#include "../display/spritebatch.h"
|
||||
#include "../display/tileset.h"
|
||||
|
||||
/**
|
||||
* Initialize the game context.
|
||||
*
|
||||
|
@ -24,4 +24,5 @@ void gameTimeUpdate(float platformDelta) {
|
||||
TIME_STATE.last = TIME_STATE.current;
|
||||
TIME_STATE.current = TIME_STATE.current + platformDelta;
|
||||
TIME_STATE.delta = TIME_STATE.current - TIME_STATE.last;
|
||||
TIME_STATE.fixedDelta = GAMETIME_FIXED_STEP;
|
||||
}
|
@ -61,7 +61,7 @@ int32_t main() {
|
||||
// Tick the engine.
|
||||
if(!gameUpdate(fDelta)) break;
|
||||
glfwSwapBuffers(window);
|
||||
sleep(0);
|
||||
sleep(0);//Fixes some weird high CPU bug, not actually necessary.
|
||||
}
|
||||
|
||||
// Game has finished running, cleanup.
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#define mathMod(a,b) (a%b+b)%b
|
||||
#define mathMax(a,b) (a<b?b:a)
|
||||
#define mathMin(a,b) (a>b?b:a)
|
||||
#define mathMod(a,b) (a%b+b)%b
|
||||
#define mathMax(a,b) (a<b?b:a)
|
||||
#define mathMin(a,b) (a>b?b:a)
|
||||
#define mathAbs(n) (n<0?-n:n)
|
28
src/util/rand.h
Normal file
28
src/util/rand.h
Normal file
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
|
||||
/**
|
||||
* Generate a number between 0 and max. (max exclusive)
|
||||
*
|
||||
* @param max Max number to generate
|
||||
* @return Number between 0 and max.
|
||||
*/
|
||||
#define u32rand(max) (rand()%max)
|
||||
#define u8rand(max) (uint8_t)u23rand(max)
|
||||
|
||||
/**
|
||||
* Generate a number between min and max. (max exclusive, min inclusive)
|
||||
*
|
||||
* @param min Min number to generate.
|
||||
* @param max Max number to generate.
|
||||
* @return Number between min and max.
|
||||
*/
|
||||
#define u32randRange(min, max) (u32rand(max-min)+min)
|
||||
#define u8randRange(min, max) (uint8_t)u32randRange(min,max)
|
@ -7,10 +7,34 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
void entityCommonMoveUpdate(entityid_t id, entity_t *entity) {
|
||||
float x, y, z, delta;
|
||||
x = entity->gridX - entity->positionX;
|
||||
y = entity->gridY - entity->positionY;
|
||||
z = entity->gridZ - entity->positionZ;
|
||||
|
||||
if(mathAbs(x) <= 0.05 && mathAbs(y) <= 0.05 && mathAbs(z) <= 0.05) {
|
||||
entity->positionX = entity->gridX;
|
||||
entity->positionY = entity->gridY;
|
||||
entity->positionZ = entity->gridZ;
|
||||
entity->state -= ENTITY_STATE_WALKING;
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Change this from easing curve to linear.
|
||||
delta = TIME_STATE.delta * ENTITY_COMMON_MOVE_SPEED;
|
||||
entity->positionX += x == 0 ? 0 : x > 0 ? delta : -delta;
|
||||
entity->positionY += y == 0 ? 0 : y > 0 ? delta : -delta;
|
||||
entity->positionZ += z == 0 ? 0 : z > 0 ? delta : -delta;
|
||||
}
|
||||
|
||||
void entityCommonMove(entityid_t id, entity_t *entity, int32_t x, int32_t y, int32_t z) {
|
||||
int32_t newX, newY, newZ, chunkIndex, tileIndex;
|
||||
tileid_t tileId;
|
||||
|
||||
// Update state.
|
||||
entity->state |= ENTITY_STATE_WALKING;
|
||||
|
||||
// Determine the new coordinates.
|
||||
newX = entity->gridX + x;
|
||||
newY = entity->gridY + y;
|
||||
@ -35,15 +59,18 @@ void entityCommonMove(entityid_t id, entity_t *entity, int32_t x, int32_t y, int
|
||||
}
|
||||
|
||||
void entityCommonRender(entityid_t id, entity_t *entity) {
|
||||
float d = TIME_STATE.delta * ENTITY_COMMON_MOVE_SPEED;
|
||||
entity->positionX += (entity->gridX - entity->positionX) * d;
|
||||
entity->positionY += (entity->gridY - entity->positionY) * d;
|
||||
entity->positionZ += (entity->gridZ - entity->positionZ) * d;
|
||||
tilesetdiv_t div = tilesetGetDivision(ENTITY_STATE.tileset, 0, entity->direction);
|
||||
|
||||
// Render sprite
|
||||
spriteBatchQuad(ENTITY_STATE.spriteBatch, -1,
|
||||
entity->positionX, entity->positionY, entity->positionZ + 0.16,
|
||||
entity->positionX, entity->positionY, entity->positionZ + 0.01,
|
||||
1, 1,
|
||||
0, 0, 1, 1
|
||||
div.x0, div.y0, div.x1, div.y1
|
||||
);
|
||||
}
|
||||
|
||||
void entityCommonTurn(entityid_t id, entity_t *entity, uint8_t dir) {
|
||||
if(entity->direction == dir) return;
|
||||
entity->direction = dir;
|
||||
entity->state = ENTITY_STATE_WALKING;
|
||||
}
|
@ -8,9 +8,14 @@
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "../../display/spritebatch.h"
|
||||
#include "../../display/tileset.h"
|
||||
#include "../map/tile.h"
|
||||
#include "../map/chunk.h"
|
||||
#include "../../util/math.h"
|
||||
|
||||
#define ENTITY_COMMON_MOVE_SPEED 10
|
||||
#define ENTITY_COMMON_MOVE_SPEED 3
|
||||
|
||||
void entityCommonMoveUpdate(entityid_t id, entity_t *entity);
|
||||
void entityCommonMove(entityid_t id, entity_t *entity, int32_t x, int32_t y, int32_t z);
|
||||
void entityCommonRender(entityid_t id, entity_t *entity);
|
||||
void entityCommonTurn(entityid_t id, entity_t *entity, uint8_t dir);
|
48
src/world/entity/entities/player.c
Normal file
48
src/world/entity/entities/player.c
Normal file
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "player.h"
|
||||
|
||||
void playerInit(entityid_t id, entity_t *entity) {
|
||||
}
|
||||
|
||||
void playerUpdate(entityid_t id, entity_t *entity) {
|
||||
// Movement
|
||||
if(entity->state & ENTITY_STATE_WALKING) {
|
||||
entityCommonMoveUpdate(id, entity);
|
||||
} else {
|
||||
if(inputIsPressed(INPUT_UP)) {
|
||||
entityCommonTurn(id, entity, ENTITY_DIRECTION_NORTH);
|
||||
} else if(inputIsPressed(INPUT_DOWN)) {
|
||||
entityCommonTurn(id, entity, ENTITY_DIRECTION_SOUTH);
|
||||
} else if(inputIsPressed(INPUT_LEFT)) {
|
||||
entityCommonTurn(id, entity, ENTITY_DIRECTION_WEST);
|
||||
} else if(inputIsPressed(INPUT_RIGHT)) {
|
||||
entityCommonTurn(id, entity, ENTITY_DIRECTION_EAST);
|
||||
|
||||
} else if(inputIsDown(INPUT_UP)) {
|
||||
entityCommonTurn(id, entity, ENTITY_DIRECTION_NORTH);
|
||||
entityCommonMove(id, entity, 0, 1, 0);
|
||||
} else if(inputIsDown(INPUT_DOWN)) {
|
||||
entityCommonTurn(id, entity, ENTITY_DIRECTION_SOUTH);
|
||||
entityCommonMove(id, entity, 0, -1, 0);
|
||||
} else if(inputIsDown(INPUT_LEFT)) {
|
||||
entityCommonTurn(id, entity, ENTITY_DIRECTION_WEST);
|
||||
entityCommonMove(id, entity, -1, 0, 0);
|
||||
} else if(inputIsDown(INPUT_RIGHT)) {
|
||||
entityCommonTurn(id, entity, ENTITY_DIRECTION_EAST);
|
||||
entityCommonMove(id, entity, 1, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void playerRender(entityid_t id, entity_t *entity) {
|
||||
entityCommonRender(id, entity);
|
||||
}
|
||||
|
||||
void playerDispose(entityid_t id, entity_t *entity) {
|
||||
}
|
@ -7,9 +7,10 @@
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "common.h"
|
||||
#include "../../input/input.h"
|
||||
#include "../common.h"
|
||||
#include "../../../input/input.h"
|
||||
|
||||
void playerInit(entityid_t entityId, entity_t *entity);
|
||||
void playerUpdate(entityid_t entityId, entity_t *entity);
|
||||
void playerRender(entityid_t entityId, entity_t *entity);
|
||||
void playerDispose(entityid_t entityId, entity_t *entity);
|
@ -10,8 +10,22 @@
|
||||
entitystate_t ENTITY_STATE;
|
||||
|
||||
void entityStateInit() {
|
||||
// Reset the entities
|
||||
memset(ENTITY_STATE.entities, 0, sizeof(entity_t) * ENTITY_COUNT);
|
||||
|
||||
// Prepare the spritebatch.
|
||||
ENTITY_STATE.spriteBatch = spriteBatchCreate(ENTITY_COUNT);
|
||||
|
||||
// Load the texture
|
||||
ENTITY_STATE.texture = assetTextureLoad(ENTITY_ASSET_TEXTURE);
|
||||
|
||||
// Divide the tileset
|
||||
ENTITY_STATE.tileset = tilesetCreate(
|
||||
ENTITY_STATE.texture->width/ENTITY_WIDTH,
|
||||
ENTITY_STATE.texture->height/ENTITY_HEIGHT,
|
||||
ENTITY_STATE.texture->width, ENTITY_STATE.texture->height,
|
||||
0,0,0,0
|
||||
);
|
||||
}
|
||||
|
||||
void entityStateRender() {
|
||||
@ -21,14 +35,16 @@ void entityStateRender() {
|
||||
// Flush the batch.
|
||||
spriteBatchFlush(ENTITY_STATE.spriteBatch);
|
||||
|
||||
// Render the entities.
|
||||
// Update and Render the entities.
|
||||
for(i = 0; i < ENTITY_COUNT; i++) {
|
||||
entity = ENTITY_STATE.entities + i;
|
||||
if(entity->type == ENTITY_TYPE_NULL) break;
|
||||
ENTITY_TYPES[entity->type].entityUpdate(i, entity);
|
||||
ENTITY_TYPES[entity->type].entityRender(i, entity);
|
||||
}
|
||||
|
||||
// Draw the sprite batch.
|
||||
shaderUseTexture(GAME_STATE.shaderWorld, ENTITY_STATE.texture);
|
||||
shaderUsePosition(GAME_STATE.shaderWorld, 0, 0, 0, 0, 0, 0);
|
||||
spriteBatchDraw(ENTITY_STATE.spriteBatch, 0, -1);
|
||||
}
|
||||
|
@ -8,8 +8,11 @@
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "entitytypes.h"
|
||||
#include "../../file/asset.h"
|
||||
#include "../../display/spritebatch.h"
|
||||
#include "../../display/shader.h"
|
||||
#include "../../display/texture.h"
|
||||
#include "../../display/tileset.h"
|
||||
|
||||
/**
|
||||
* Initializes the entity state system.
|
||||
|
@ -9,12 +9,18 @@
|
||||
|
||||
entitytype_t ENTITY_TYPES[ENTITY_TYPE_COUNT] = {
|
||||
// ENTITY_TYPE_NULL
|
||||
{ .entityInit = NULL, .entityUpdate = NULL, .entityDispose = NULL },
|
||||
{
|
||||
.entityInit = NULL,
|
||||
.entityUpdate = NULL,
|
||||
.entityRender = NULL,
|
||||
.entityDispose = NULL
|
||||
},
|
||||
|
||||
// ENTITY_TYPE_PLAYER
|
||||
{
|
||||
.entityInit = &playerInit,
|
||||
.entityUpdate = &playerUpdate,
|
||||
.entityRender = &playerRender,
|
||||
.entityDispose = &playerDispose
|
||||
}
|
||||
};
|
@ -7,4 +7,4 @@
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "player.h"
|
||||
#include "entities/player.h"
|
@ -1,29 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "player.h"
|
||||
|
||||
void playerInit(entityid_t id, entity_t *entity) {
|
||||
}
|
||||
|
||||
void playerUpdate(entityid_t id, entity_t *entity) {
|
||||
if(inputIsPressed(INPUT_UP)) {
|
||||
entityCommonMove(id, entity, 0, 1, 0);
|
||||
} else if(inputIsPressed(INPUT_DOWN)) {
|
||||
entityCommonMove(id, entity, 0, -1, 0);
|
||||
} else if(inputIsPressed(INPUT_LEFT)) {
|
||||
entityCommonMove(id, entity, -1, 0, 0);
|
||||
} else if(inputIsPressed(INPUT_RIGHT)) {
|
||||
entityCommonMove(id, entity, 1, 0, 0);
|
||||
}
|
||||
|
||||
// Render sprite
|
||||
entityCommonRender(id, entity);
|
||||
}
|
||||
|
||||
void playerDispose(entityid_t id, entity_t *entity) {
|
||||
}
|
@ -12,10 +12,12 @@ void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z) {
|
||||
tiledef_t *tileDef;
|
||||
int32_t i, indiceCount, verticeCount, tx, ty, tz;
|
||||
|
||||
chunk->tiles[0] = 1;
|
||||
chunk->tiles[1] = 1;
|
||||
chunk->tiles[16] = 1;
|
||||
chunk->tiles[17] = 1;
|
||||
for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
|
||||
for(tx = 0; tx < CHUNK_WIDTH; tx++) {
|
||||
if(z != 0) break;
|
||||
chunk->tiles[ty*CHUNK_WIDTH + tx] = ty == 5 ? 2 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Start by loading the tiles and figuring out how big we need to make the
|
||||
// primitive that the chunk uses.
|
||||
|
@ -13,6 +13,17 @@ void worldInit() {
|
||||
}
|
||||
|
||||
void worldRender() {
|
||||
if(ENTITY_STATE.entities[0].type != ENTITY_TYPE_NULL) {
|
||||
cameraLookAt(&GAME_STATE.cameraWorld,
|
||||
ENTITY_STATE.entities[0].positionX,
|
||||
ENTITY_STATE.entities[0].positionY - 0.5,
|
||||
ENTITY_STATE.entities[0].positionZ + 7,
|
||||
|
||||
ENTITY_STATE.entities[0].positionX,
|
||||
ENTITY_STATE.entities[0].positionY,
|
||||
ENTITY_STATE.entities[0].positionZ
|
||||
);
|
||||
}
|
||||
mapRender();
|
||||
entityStateRender();
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <dawn/dawn.h>
|
||||
#include "map/map.h"
|
||||
#include "entity/entity.h"
|
||||
#include "../display/camera.h"
|
||||
|
||||
void worldInit();
|
||||
void worldRender();
|
||||
|
Reference in New Issue
Block a user