Disconnected world code temporarily.
This commit is contained in:
79
temp/include/world/entity/entity.h
Normal file
79
temp/include/world/entity/entity.h
Normal file
@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "../../display/spritebatch.h"
|
||||
#include "../../display/texture.h"
|
||||
#include "../../display/tileset.h"
|
||||
|
||||
/** Entity Texture Information */
|
||||
#define ENTITY_ASSET_TEXTURE "world/entity.png"
|
||||
#define ENTITY_WIDTH 32
|
||||
#define ENTITY_HEIGHT ENTITY_WIDTH
|
||||
|
||||
/** Entity ID Definitions */
|
||||
#define ENTITY_TYPE_NULL 0x00
|
||||
#define ENTITY_TYPE_PLAYER 0x01
|
||||
|
||||
/** Max count of entities in the world */
|
||||
#define ENTITY_COUNT 64
|
||||
|
||||
/** Count of different types of entities */
|
||||
#define ENTITY_TYPE_COUNT ENTITY_TYPE_PLAYER + 1
|
||||
|
||||
#define ENTITY_DIRECTION_SOUTH 0x00
|
||||
#define ENTITY_DIRECTION_NORTH 0x01
|
||||
#define ENTITY_DIRECTION_WEST 0x02
|
||||
#define ENTITY_DIRECTION_EAST 0x03
|
||||
|
||||
#define ENTITY_STATE_WALKING 0x01
|
||||
|
||||
/** Unique Entity ID */
|
||||
typedef uint8_t entityid_t;
|
||||
|
||||
/** Unique Entity ID for the Entity Type */
|
||||
typedef uint8_t entitytypeid_t;
|
||||
|
||||
/** Entity Definition */
|
||||
typedef struct {
|
||||
entitytypeid_t type;
|
||||
int32_t gridX, gridY, gridZ;
|
||||
int32_t oldGridX, oldGridY, oldGridZ;
|
||||
float positionX, positionY, positionZ;
|
||||
uint8_t direction;
|
||||
uint32_t state;
|
||||
} entity_t;
|
||||
|
||||
/** Definition for an entity type */
|
||||
typedef struct {
|
||||
void (*entityInit)(entityid_t entityId, entity_t *entity);
|
||||
void (*entityUpdate)(entityid_t entityId, entity_t *entity);
|
||||
void (*entityRender)(entityid_t entityId, entity_t *entity);
|
||||
void (*entityDispose)(entityid_t entityId, entity_t *entity);
|
||||
} entitytype_t;
|
||||
|
||||
/** Entity State Management */
|
||||
typedef struct {
|
||||
/** Entities within the state */
|
||||
entity_t entities[ENTITY_COUNT];
|
||||
|
||||
/** Sprite Batch in the state */
|
||||
spritebatch_t *spriteBatch;
|
||||
|
||||
/** Texture for entities */
|
||||
texture_t *texture;
|
||||
|
||||
/** Divided Tileset for entities */
|
||||
tileset_t *tileset;
|
||||
} entitystate_t;
|
||||
|
||||
/** Global Entity State */
|
||||
extern entitystate_t ENTITY_STATE;
|
||||
|
||||
/** Global Entity Type Definitions */
|
||||
extern entitytype_t ENTITY_TYPES[ENTITY_TYPE_COUNT];
|
33
temp/include/world/map/chunk.h
Normal file
33
temp/include/world/map/chunk.h
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "../../display/primitive.h"
|
||||
#include "tile.h"
|
||||
|
||||
/** When loading a chunk, how many chars to offset (ASCII char to byte) */
|
||||
#define CHUNK_TILE_LOAD_ASCII 48
|
||||
|
||||
/** Width (in tiles) of chunks. */
|
||||
#define CHUNK_WIDTH 16
|
||||
/** Height (in tiles) of chunks. */
|
||||
#define CHUNK_HEIGHT 16
|
||||
/** Depth (in tiles) of chunks. */
|
||||
#define CHUNK_DEPTH 8
|
||||
/** Count of tiles in the chunk. */
|
||||
#define CHUNK_TILE_COUNT CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH
|
||||
|
||||
/** Representation of a chunk, a group of tiles that can be buffered around. */
|
||||
typedef struct {
|
||||
/** Position (in absolute chunk coordinates) of this chunk */
|
||||
int32_t x, y, z;
|
||||
|
||||
/** Array of tiles within the chunk */
|
||||
tileid_t tiles[CHUNK_TILE_COUNT];
|
||||
|
||||
/** Ready to be rendered chunk 3d primitive */
|
||||
primitive_t *primitive;
|
||||
} chunk_t;
|
46
temp/include/world/map/map.h
Normal file
46
temp/include/world/map/map.h
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "tile.h"
|
||||
#include "chunk.h"
|
||||
#include "../../display/texture.h"
|
||||
#include "../../display/tileset.h"
|
||||
|
||||
/** Width of map (in chunks) */
|
||||
#define MAP_WIDTH 3
|
||||
/** Height of map (in chunks) */
|
||||
#define MAP_HEIGHT MAP_WIDTH
|
||||
/** Depth of map (in chunks) */
|
||||
#define MAP_DEPTH 2
|
||||
/** Count of chunks in the world */
|
||||
#define MAP_CHUNK_COUNT MAP_WIDTH * MAP_HEIGHT * MAP_DEPTH
|
||||
|
||||
#define MAP_ASSET_TEXTURE "world/tileset.png"
|
||||
|
||||
typedef struct {
|
||||
/** Tile definitions */
|
||||
tiledef_t *tileDefinitions;
|
||||
|
||||
/** Tileset predivided */
|
||||
tileset_t *tileset;
|
||||
|
||||
/** Texture of the tileset */
|
||||
texture_t *texture;
|
||||
|
||||
/** Current (chunk) coordinates of the first chunk in the chunk list */
|
||||
int32_t x, y, z;
|
||||
|
||||
/** Current chunk list, ordered */
|
||||
chunk_t *chunkList[MAP_CHUNK_COUNT];
|
||||
|
||||
/** Chunk array, unordered */
|
||||
chunk_t chunks[MAP_CHUNK_COUNT];
|
||||
} map_t;
|
||||
|
||||
extern map_t MAP_STATE;
|
29
temp/include/world/map/tile.h
Normal file
29
temp/include/world/map/tile.h
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
|
||||
/** Width of a tile (in pixels) */
|
||||
#define TILE_WIDTH 16
|
||||
/** Height of a tile (in pixels) */
|
||||
#define TILE_HEIGHT 16
|
||||
/** What a NULL tile is represented as. */
|
||||
#define TILE_NULL 0x00
|
||||
|
||||
/** Bitwise Flags from tiles. */
|
||||
typedef uint8_t tileflag_t;
|
||||
|
||||
/** Tile ID */
|
||||
typedef uint8_t tileid_t;
|
||||
|
||||
/** Representation of the information of a tile within a tilemap. */
|
||||
typedef struct {
|
||||
/** Flags of the tile */
|
||||
tileflag_t flags;
|
||||
|
||||
/** How many indices and vertices a tile with this definition has. */
|
||||
int32_t indiceCount, verticeCount;
|
||||
} tiledef_t;
|
9
temp/include/world/world.h
Normal file
9
temp/include/world/world.h
Normal file
@ -0,0 +1,9 @@
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "map/chunk.h"
|
||||
#include "map/map.h"
|
76
temp/src/world/entity/common.c
Normal file
76
temp/src/world/entity/common.c
Normal file
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#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;
|
||||
newZ = entity->gridZ + z;
|
||||
|
||||
// Can we move there, tile check first then entity check.
|
||||
tilegetresult_t result = tileGet(newX, newY, newZ);
|
||||
chunkIndex = chunkGet(result.chunkX, result.chunkY, result.chunkZ);
|
||||
if(chunkIndex == -1) return;
|
||||
tileIndex = chunkGetTile(result.localX, result.localY, result.localZ);
|
||||
|
||||
tileId = MAP_STATE.chunkList[chunkIndex]->tiles[tileIndex];
|
||||
if(tileId == TILE_NULL) return;
|
||||
|
||||
// Update the old and new positions
|
||||
entity->oldGridX = entity->gridX;
|
||||
entity->oldGridY = entity->gridY;
|
||||
entity->oldGridZ = entity->gridZ;
|
||||
entity->gridX = newX;
|
||||
entity->gridY = newY;
|
||||
entity->gridZ = newZ;
|
||||
}
|
||||
|
||||
void entityCommonRender(entityid_t id, entity_t *entity) {
|
||||
tilesetdiv_t div = tilesetGetDivision(ENTITY_STATE.tileset, 0, entity->direction);
|
||||
|
||||
// Render sprite
|
||||
spriteBatchQuad(ENTITY_STATE.spriteBatch, -1,
|
||||
entity->positionX, entity->positionY, entity->positionZ + 0.01,
|
||||
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;
|
||||
}
|
20
temp/src/world/entity/common.h
Normal file
20
temp/src/world/entity/common.h
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* 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 "../../display/spritebatch.h"
|
||||
#include "../../display/tileset.h"
|
||||
#include "../map/tile.h"
|
||||
#include "../map/chunk.h"
|
||||
|
||||
#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
temp/src/world/entity/entities/player.c
Normal file
48
temp/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) {
|
||||
}
|
16
temp/src/world/entity/entities/player.h
Normal file
16
temp/src/world/entity/entities/player.h
Normal file
@ -0,0 +1,16 @@
|
||||
/**
|
||||
* 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 "../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);
|
84
temp/src/world/entity/entity.c
Normal file
84
temp/src/world/entity/entity.c
Normal file
@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "entity.h"
|
||||
|
||||
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() {
|
||||
entityid_t i;
|
||||
entity_t *entity;
|
||||
|
||||
// Flush the batch.
|
||||
spriteBatchFlush(ENTITY_STATE.spriteBatch);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
void entityStateDispose() {
|
||||
entityid_t i;
|
||||
entity_t *entity;
|
||||
|
||||
for(i = 0; i < ENTITY_COUNT; i++) {
|
||||
entity = ENTITY_STATE.entities + i;
|
||||
if(entity->type == ENTITY_TYPE_NULL) break;
|
||||
entityDispose(i);
|
||||
}
|
||||
|
||||
spriteBatchDispose(ENTITY_STATE.spriteBatch);
|
||||
}
|
||||
|
||||
void entityInit(entityid_t id, entitytypeid_t type) {
|
||||
entity_t *entity = ENTITY_STATE.entities + id;
|
||||
entity->type = type;
|
||||
|
||||
// Reset values
|
||||
entity->gridX = entity->gridY = entity->gridZ = 0;
|
||||
entity->oldGridX = entity->oldGridY = entity->oldGridZ = 0;
|
||||
entity->positionX = entity->positionY = entity->positionZ = 0;
|
||||
|
||||
// Init
|
||||
if(ENTITY_TYPES[type].entityInit == NULL) return;
|
||||
ENTITY_TYPES[type].entityInit(id, entity);
|
||||
}
|
||||
|
||||
void entityDispose(entityid_t id) {
|
||||
entity_t *entity = ENTITY_STATE.entities + id;
|
||||
entity->type = ENTITY_TYPE_NULL;
|
||||
if(ENTITY_TYPES[entity->type].entityDispose == NULL) return;
|
||||
ENTITY_TYPES[entity->type].entityDispose(id, entity);
|
||||
}
|
45
temp/src/world/entity/entity.h
Normal file
45
temp/src/world/entity/entity.h
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* 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 "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.
|
||||
*/
|
||||
void entityStateInit();
|
||||
|
||||
/**
|
||||
* Render the entity state system.
|
||||
*/
|
||||
void entityStateRender();
|
||||
|
||||
/**
|
||||
* Dispose and clean up the entity state system.
|
||||
*/
|
||||
void entityStateDispose();
|
||||
|
||||
/**
|
||||
* Initializes an entity into the entity state.
|
||||
*
|
||||
* @param id The Entity ID within the entity state system.
|
||||
* @param type The Entity type to initialize.
|
||||
*/
|
||||
void entityInit(entityid_t id, entitytypeid_t type);
|
||||
|
||||
/**
|
||||
* Disposes the entity from the entity state.
|
||||
*
|
||||
* @param id The entity id to dispose.
|
||||
*/
|
||||
void entityDispose(entityid_t id);
|
26
temp/src/world/entity/entitytypes.c
Normal file
26
temp/src/world/entity/entitytypes.c
Normal file
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "entitytypes.h"
|
||||
|
||||
entitytype_t ENTITY_TYPES[ENTITY_TYPE_COUNT] = {
|
||||
// ENTITY_TYPE_NULL
|
||||
{
|
||||
.entityInit = NULL,
|
||||
.entityUpdate = NULL,
|
||||
.entityRender = NULL,
|
||||
.entityDispose = NULL
|
||||
},
|
||||
|
||||
// ENTITY_TYPE_PLAYER
|
||||
{
|
||||
.entityInit = &playerInit,
|
||||
.entityUpdate = &playerUpdate,
|
||||
.entityRender = &playerRender,
|
||||
.entityDispose = &playerDispose
|
||||
}
|
||||
};
|
10
temp/src/world/entity/entitytypes.h
Normal file
10
temp/src/world/entity/entitytypes.h
Normal file
@ -0,0 +1,10 @@
|
||||
/**
|
||||
* 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 "entities/player.h"
|
90
temp/src/world/map/chunk.c
Normal file
90
temp/src/world/map/chunk.c
Normal file
@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "chunk.h"
|
||||
|
||||
void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z) {
|
||||
tileid_t tileId;
|
||||
tiledef_t *tileDef;
|
||||
int32_t i, indiceCount, verticeCount, tx, ty, tz;
|
||||
|
||||
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.
|
||||
indiceCount = 0, verticeCount = 0;
|
||||
for(i = 0; i < CHUNK_TILE_COUNT; i++) {
|
||||
//TODO: Actually load the tileId here
|
||||
tileId = chunk->tiles[i];
|
||||
if(tileId == TILE_NULL) continue;
|
||||
|
||||
// Increment the primitive size.
|
||||
tileDef = MAP_STATE.tileDefinitions + tileId;
|
||||
verticeCount += tileDef->verticeCount;
|
||||
indiceCount += tileDef->indiceCount;
|
||||
}
|
||||
|
||||
// Do we even need to create a primitive?
|
||||
if(indiceCount == 0) return;
|
||||
chunk->primitive = primitiveCreate(verticeCount, indiceCount);
|
||||
|
||||
// Render each tile. The ZYX order is important for ordering.
|
||||
i = 0;
|
||||
verticeCount = 0, indiceCount = 0;
|
||||
for(tz = 0; tz < CHUNK_DEPTH; tz++) {
|
||||
for(ty = 0; ty < CHUNK_HEIGHT; ty++) {
|
||||
for(tx = 0; tx < CHUNK_WIDTH; tx++) {
|
||||
tileId = chunk->tiles[i];
|
||||
if(tileId == TILE_NULL) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
tileDef = MAP_STATE.tileDefinitions + tileId;
|
||||
|
||||
tileRender(
|
||||
chunk, tileId, tileDef,
|
||||
i, tx, ty, tz,
|
||||
verticeCount, indiceCount
|
||||
);
|
||||
|
||||
// Prepare for the next render.
|
||||
verticeCount += tileDef->verticeCount;
|
||||
indiceCount += tileDef->indiceCount;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void chunkUnload(chunk_t *chunk) {
|
||||
// Load chunks to zero. TODO: Necessary?
|
||||
memset(chunk->tiles, TILE_NULL, CHUNK_TILE_COUNT);
|
||||
|
||||
// Unload the primitive. TODO: Can we salvage this and resize instead?
|
||||
if(chunk->primitive == NULL) return;
|
||||
primitiveDispose(chunk->primitive);
|
||||
chunk->primitive = NULL;
|
||||
}
|
||||
|
||||
int32_t chunkGet(int32_t x, int32_t y, int32_t z) {
|
||||
int32_t i = (
|
||||
mathMod(x - MAP_STATE.x, MAP_WIDTH) +
|
||||
(mathMod(y - MAP_STATE.y, MAP_HEIGHT) * MAP_WIDTH) +
|
||||
(mathMod(z - MAP_STATE.z, MAP_DEPTH) * MAP_WIDTH * MAP_HEIGHT)
|
||||
);
|
||||
if(i < 0 || i > MAP_CHUNK_COUNT) return -1;
|
||||
return i;
|
||||
}
|
||||
|
||||
int32_t chunkGetTile(int32_t x, int32_t y, int32_t z) {
|
||||
return x + (y * CHUNK_WIDTH) + (z * CHUNK_WIDTH * CHUNK_HEIGHT);
|
||||
}
|
48
temp/src/world/map/chunk.h
Normal file
48
temp/src/world/map/chunk.h
Normal file
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* 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 "../../display/primitive.h"
|
||||
#include "tile.h"
|
||||
|
||||
/**
|
||||
* Loads a given chunk.
|
||||
*
|
||||
* @param chunk The chunk to load.
|
||||
* @param x X of the chunk.
|
||||
* @param y Y of the chunk.
|
||||
* @param z Z of the chunk.
|
||||
*/
|
||||
void chunkLoad(chunk_t *chunk, int32_t x, int32_t y, int32_t z);
|
||||
|
||||
/**
|
||||
* Unload a given chunk.
|
||||
*
|
||||
* @param chunk Chunk to unload.
|
||||
*/
|
||||
void chunkUnload(chunk_t *chunk);
|
||||
|
||||
/**
|
||||
* Gets the chunk index from an absolute coordinate.
|
||||
*
|
||||
* @param x Absolute chunk X.
|
||||
* @param y Absolute chunk Y.
|
||||
* @param z Absolute chunk Z.
|
||||
* @returns The index for that chunk. -1 if out of bounds of the current list.
|
||||
*/
|
||||
int32_t chunkGet(int32_t x, int32_t y, int32_t z);
|
||||
|
||||
/**
|
||||
* Gets the tile index from a local tile coordinate.
|
||||
*
|
||||
* @param x The local X coordinate of the tile.
|
||||
* @param y The local Y coordinate of the tile.
|
||||
* @param z The local Z coordinate of the tile.
|
||||
* @return The index within the chunk that the tile resides.
|
||||
*/
|
||||
int32_t chunkGetTile(int32_t x, int32_t y, int32_t z);
|
171
temp/src/world/map/map.c
Normal file
171
temp/src/world/map/map.c
Normal file
@ -0,0 +1,171 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "map.h"
|
||||
|
||||
map_t MAP_STATE;
|
||||
|
||||
void mapInit() {
|
||||
int32_t i, z, x, y;
|
||||
chunk_t *chunk;
|
||||
|
||||
// Reset map
|
||||
MAP_STATE.x = MAP_STATE.y = MAP_STATE.z = 0;
|
||||
|
||||
// Load the texture
|
||||
MAP_STATE.texture = assetTextureLoad(MAP_ASSET_TEXTURE);
|
||||
|
||||
// Load the tileset
|
||||
MAP_STATE.tileset = tilesetCreate(
|
||||
MAP_STATE.texture->width/TILE_WIDTH, MAP_STATE.texture->height/TILE_HEIGHT,
|
||||
MAP_STATE.texture->width, MAP_STATE.texture->height,
|
||||
0, 0, 0, 0
|
||||
);
|
||||
|
||||
// Prepare the tile definitions
|
||||
MAP_STATE.tileDefinitions = calloc(
|
||||
MAP_STATE.tileset->count,sizeof(tiledef_t)
|
||||
);
|
||||
|
||||
// TODO: Load the tile definitions here.
|
||||
for(i = 0; i < MAP_STATE.tileset->count; i++) {
|
||||
(MAP_STATE.tileDefinitions+i)->verticeCount = 4;
|
||||
(MAP_STATE.tileDefinitions+i)->indiceCount = 6;
|
||||
}
|
||||
|
||||
// Load each of the chunks. The ZYX order is important for ordering.
|
||||
i = 0;
|
||||
for(z = 0; z < MAP_DEPTH; z++) {
|
||||
for(y = 0; y < MAP_HEIGHT; y++) {
|
||||
for(x = 0; x < MAP_WIDTH; x++) {
|
||||
chunk = MAP_STATE.chunks + i;
|
||||
MAP_STATE.chunkList[i] = chunk;
|
||||
|
||||
// Load initial chunk state.
|
||||
chunk->x = x;
|
||||
chunk->y = y;
|
||||
chunk->z = z;
|
||||
chunk->primitive = NULL;
|
||||
|
||||
// Load the chunk.
|
||||
chunkLoad(chunk, x, y, z);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mapRender() {
|
||||
int32_t i;
|
||||
float x, y, z;
|
||||
chunk_t *chunk;
|
||||
|
||||
// Bind the texture
|
||||
shaderUseTexture(GAME_STATE.shaderWorld, MAP_STATE.texture);
|
||||
|
||||
// Render each chunk.
|
||||
// TODO: Do we need to render every chunk? (screen area)
|
||||
for(i = 0; i < MAP_CHUNK_COUNT; i++) {
|
||||
chunk = MAP_STATE.chunkList[i];
|
||||
if(chunk->primitive == NULL) continue;
|
||||
|
||||
// Offset the primitive (into world space)
|
||||
x = (chunk->x * CHUNK_WIDTH);
|
||||
y = (chunk->y * CHUNK_HEIGHT);
|
||||
z = (chunk->z * CHUNK_DEPTH);
|
||||
shaderUsePosition(GAME_STATE.shaderWorld, x, y, z, 0, 0, 0);
|
||||
primitiveDraw(chunk->primitive, 0, chunk->primitive->indiceCount);
|
||||
}
|
||||
}
|
||||
|
||||
void mapDispose() {
|
||||
int32_t i;
|
||||
chunk_t *chunk;
|
||||
|
||||
// Unload the chunks
|
||||
for(i = 0; i < MAP_CHUNK_COUNT; i++) {
|
||||
chunk = MAP_STATE.chunkList[i];
|
||||
chunkUnload(chunk);
|
||||
}
|
||||
|
||||
tilesetDispose(MAP_STATE.tileset);
|
||||
textureDispose(MAP_STATE.texture);
|
||||
}
|
||||
|
||||
void mapShift(int32_t x, int32_t y, int32_t z) {
|
||||
int32_t i,
|
||||
/** New List Coordinates */
|
||||
lx, ly, lz,
|
||||
/** New Local Chunk Coordinates */
|
||||
nx, ny, nz,
|
||||
/** New Absolute Chunk Coordinates */
|
||||
ax, ay, az,
|
||||
/** New Chunk Index */
|
||||
ni,
|
||||
/** Precalculated width * height */
|
||||
wh
|
||||
;
|
||||
chunk_t *chunk;
|
||||
chunk_t *chunkList[MAP_CHUNK_COUNT];
|
||||
|
||||
// Calculate the new chunklist coordinates
|
||||
lx = MAP_STATE.x + x;
|
||||
ly = MAP_STATE.y + y;
|
||||
lz = MAP_STATE.z + z;
|
||||
|
||||
// Precalc width * height.
|
||||
wh = MAP_WIDTH * MAP_HEIGHT;
|
||||
|
||||
for(i = 0; i < MAP_CHUNK_COUNT; i++) {
|
||||
chunk = MAP_STATE.chunkList[i];
|
||||
|
||||
// Calculate the new local positions for the chunk.
|
||||
nx = mathMod(chunk->x - MAP_STATE.x - x, MAP_WIDTH);
|
||||
ny = mathMod(chunk->y - MAP_STATE.y - y, MAP_HEIGHT);
|
||||
nz = mathMod(chunk->z - MAP_STATE.z - z, MAP_DEPTH);
|
||||
|
||||
// Load the chunk if we need to. We also use this to calculate new absolutes
|
||||
if(
|
||||
(ax = lx + nx) != chunk->x ||
|
||||
(ly + ny) != chunk->y ||
|
||||
(lz + nz) != chunk->z
|
||||
) {
|
||||
// Calculate those things that may have not been done within the if
|
||||
ay = ly + ny;
|
||||
az = lz + nz;
|
||||
|
||||
// Load new chunk.
|
||||
chunkUnload(chunk);
|
||||
chunkLoad(chunk, ax, ay, az);
|
||||
|
||||
// Update the absolute coordinates.
|
||||
chunk->x = ax;
|
||||
chunk->y = ay;
|
||||
chunk->z = az;
|
||||
}
|
||||
|
||||
// Now, based off those new local positions, calculate the new index.
|
||||
ni = (
|
||||
nx +
|
||||
(ny * MAP_WIDTH) +
|
||||
(nz * wh)
|
||||
);
|
||||
chunkList[ni] = chunk;
|
||||
}
|
||||
|
||||
// Update Absolutes.
|
||||
MAP_STATE.x = lx;
|
||||
MAP_STATE.y = ly;
|
||||
MAP_STATE.z = lz;
|
||||
|
||||
// Now copy that array over.
|
||||
memcpy(MAP_STATE.chunkList, chunkList, sizeof(chunk_t *) * MAP_CHUNK_COUNT);
|
||||
}
|
||||
|
||||
void mapAlign(int32_t x, int32_t y, int32_t z) {
|
||||
mapShift(x - MAP_STATE.x, y - MAP_STATE.y, z - MAP_STATE.z);
|
||||
}
|
45
temp/src/world/map/map.h
Normal file
45
temp/src/world/map/map.h
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* 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 "../../display/tileset.h"
|
||||
#include "../../file/asset.h"
|
||||
#include "chunk.h"
|
||||
|
||||
/**
|
||||
* Initializes the world map.
|
||||
*/
|
||||
void mapInit();
|
||||
|
||||
/**
|
||||
* Renders the map object to the graphics device.
|
||||
*/
|
||||
void mapRender();
|
||||
|
||||
/**
|
||||
* Cleans the previously loaded world map.
|
||||
*/
|
||||
void mapDispose();
|
||||
|
||||
/**
|
||||
* Shift the map chunk list along a set of axis (in absolute space).
|
||||
*
|
||||
* @param x X movement to shift chunks along.
|
||||
* @param y Y movement to shift chunks along.
|
||||
* @param z Z movement to shift chunks along.
|
||||
*/
|
||||
void mapShift(int32_t x, int32_t y, int32_t z);
|
||||
|
||||
/**
|
||||
* Align the map chunk list (in absolute space).
|
||||
*
|
||||
* @param x X movement to shift chunks along.
|
||||
* @param y Y movement to shift chunks along.
|
||||
* @param z Z movement to shift chunks along.
|
||||
*/
|
||||
void mapAlign(int32_t x, int32_t y, int32_t z);
|
37
temp/src/world/map/tile.c
Normal file
37
temp/src/world/map/tile.c
Normal file
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "tile.h"
|
||||
|
||||
void tileRender(
|
||||
chunk_t *chunk, tileid_t id, tiledef_t *tileDef,
|
||||
int32_t i, int32_t x, int32_t y, int32_t z,
|
||||
int32_t verticeStart, int32_t indiceStart
|
||||
) {
|
||||
tilesetdiv_t *div = (MAP_STATE.tileset->divisions + id);
|
||||
quadBuffer(chunk->primitive, z,
|
||||
x, y, div->x0, div->y0,
|
||||
x+1, y+1, div->x1, div->y1,
|
||||
verticeStart, indiceStart
|
||||
);
|
||||
}
|
||||
|
||||
tilegetresult_t tileGet(int32_t x, int32_t y, int32_t z) {
|
||||
tilegetresult_t result;
|
||||
|
||||
// First, determine the chunk that I belong to.
|
||||
result.chunkX = x / CHUNK_WIDTH;
|
||||
result.chunkY = y / CHUNK_HEIGHT;
|
||||
result.chunkZ = z / CHUNK_DEPTH;
|
||||
|
||||
// And determine the local coordinates
|
||||
result.localX = mathMod(x, CHUNK_WIDTH);
|
||||
result.localY = mathMod(y, CHUNK_HEIGHT);
|
||||
result.localZ = mathMod(z, CHUNK_DEPTH);
|
||||
|
||||
return result;
|
||||
}
|
22
temp/src/world/map/tile.h
Normal file
22
temp/src/world/map/tile.h
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* 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 "../../display/primitives/quad.h"
|
||||
|
||||
typedef struct {
|
||||
int32_t chunkX, chunkY, chunkZ, localX, localY, localZ;
|
||||
} tilegetresult_t;
|
||||
|
||||
void tileRender(
|
||||
chunk_t *chunk, tileid_t id, tiledef_t *tileDef,
|
||||
int32_t i, int32_t x, int32_t y, int32_t z,
|
||||
int32_t verticeStart, int32_t indiceStart
|
||||
);
|
||||
|
||||
tilegetresult_t tileGet(int32_t x, int32_t y, int32_t z);
|
34
temp/src/world/world.c
Normal file
34
temp/src/world/world.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 "world.h"
|
||||
|
||||
void worldInit() {
|
||||
mapInit();
|
||||
entityStateInit();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void worldDispose() {
|
||||
entityStateDispose();
|
||||
mapDispose();
|
||||
}
|
16
temp/src/world/world.h
Normal file
16
temp/src/world/world.h
Normal file
@ -0,0 +1,16 @@
|
||||
/**
|
||||
* 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 "map/map.h"
|
||||
#include "entity/entity.h"
|
||||
#include "../display/camera.h"
|
||||
|
||||
void worldInit();
|
||||
void worldRender();
|
||||
void worldDispose();
|
Reference in New Issue
Block a user