timesynk/engine/states/state_test.c

195 lines
5.2 KiB
C

/******
******/
#include "state_test.h"
#include "../tile.h"
#include "../map.h"
#include "../../common/data.h"
#include "../timer.h"
/*
================================
void initTestState()
Called once when switching to this state. Used to load whatever data used during process.
================================
*/
void initTestState() {
if ((test_map = newMap(32, 32, 4)) == NULL) {
printf("ERROR: failed to allocate map!\n");
stopRunning();
return;
}
int x,y,z;
for (x=0;x < test_map->width;x++) {
for (y=0;y < test_map->height;y++) {
addTileToMap(test_map, newTile(getTileDataById(g_tile_data, 2, 0)), x, y, 0);
}
}
for (x=8;x < 9;x++) {
for (y=0;y < test_map->height;y++) {
addTileToMap(test_map, newTile(getTileDataById(g_tile_data, 3, 0)), x, y, 1);
}
}
int i = 0;
z = 0;
x = 0;
for (i=0;i < 3;i++) {
for (z = 0; z < 8;z++) {
//for(x = 12;x < 16;x++) {
//test_map->tiles[x][9][0]->next = newTile(getTileDataById(g_tile_data, 7, i));
addTileToMap(test_map, newTile(getTileDataById(g_tile_data, 8, i)), x+i, 9, z);
//}
}
}
test_player_tile = newTile(getTileDataById(g_tile_data, 0, 0));
test_map->tiles[2][2][1] = test_player_tile;
key_press[0] = 0;
key_press[1] = 0;
key_press[2] = 0;
key_press[3] = 0;
test_accumulator = 0;
s_accumulator = 0;
g_freeState = freeTestState;
g_handleState = handleTestState;
g_processState = processTestState;
g_renderState = renderTestState;
}
/*
================================
void freeTestState()
Called when switching out of this state. Free any data loading during init or process here.
================================
*/
void freeTestState() {
freeMap(test_map, 0);
//freeTile(test_player_tile);
}
/*
================================
void processState(int delta)
This function is our state's processing loop. It takes delta since last process in nanoseconds. This is where game logic occurs.
================================
*/
void processTestState(int delta) {
s_accumulator += delta;
while(s_accumulator >= g_tick_time.n) {
if (key_press[0] == 1) {
test_player_tile->set = 4;
moveTile(test_map, test_player_tile, -0.5f, 0.0f, 0.0f);
//moveTile(test_map, test_player_tile, -1.0f, 0.0f, 0.0f);
}
if (key_press[1] == 1) {
test_player_tile->set = 5;
moveTile(test_map, test_player_tile, 0.0f, 0.5f, 0.0f);
//moveTile(test_map, test_player_tile, 0.0f, 1.0f, 0.0f);
}
if (key_press[2] == 1) {
test_player_tile->set = 6;
moveTile(test_map, test_player_tile, 0.5f, 0.0f, 0.0f);
//moveTile(test_map, test_player_tile, 1.0f, 0.0f, 0.0f);
}
if (key_press[3] == 1) {
test_player_tile->set = 7;
moveTile(test_map, test_player_tile, 0.0f, -0.5f, 0.0f);
//moveTile(test_map, test_player_tile, 0.0f, -1.0f, 0.0f);
}
if (test_accumulator > 4) {
if (test_player_tile->frame < 3) {
test_player_tile->frame += 1;
} else {
test_player_tile->frame = 0;
}
test_accumulator = 0;
}
test_accumulator++;
s_accumulator -= g_tick_time.n;
}
}
/*
================================
void renderTestState()
Our render state, called a variable amount of times per second. Make render calls here, bb.
================================
*/
void renderTestState() {
int x, y, z;
for(z=0;z < test_map->depth;z++) {
for(y=0;y < test_map->height;y++) {
for(x=test_map->width-1;x >= 0;x--) {
struct Tile *tile = test_map->tiles[x][y][z];
while(tile != NULL) {
//printf("%f\n", tile->x);
//int id = tile->data->id + (tile->set * 4) + tile->frame;
int id = tile->data->id;
int pix_x = tile->x * g_module_spritesheets[tile->data->tid]->s_width;
int pix_y = (tile->y * g_module_spritesheets[tile->data->tid]->s_height)/2;
pix_y -= (tile->z * (g_module_spritesheets[tile->data->tid]->s_height/4));
g_renderSprite(g_module_spritesheets[tile->data->tid], g_screen, id, pix_x, pix_y);
tile = tile->next;
}
}
}
}
}
/*
================================
void handleTestState(struct TSEvent event)
This function handles user input. Likely to be removed and merged with processState.
================================
*/
void handleTestState(struct TSEvent event) {
switch(event.type) {
case TS_KEYBOARD:
switch(event.keyboard.key.sym) {
case 276: // left
if (event.keyboard.state == TS_KEYDOWN) {
key_press[0] = 1;
} else {
key_press[0] = 0;
}
break;
case 274: // down
if (event.keyboard.state == TS_KEYDOWN) {
key_press[1] = 1;
} else {
key_press[1] = 0;
}
break;
case 275: // right
if (event.keyboard.state == TS_KEYDOWN) {
key_press[2] = 1;
} else {
key_press[2] = 0;
}
break;
case 273: // up
if (event.keyboard.state == TS_KEYDOWN) {
key_press[3] = 1;
} else {
key_press[3] = 0;
}
break;
}
break;
case TS_MOUSECLICK:
break;
default:
break;
}
}