Player character now moves.
This commit is contained in:
138
src/in_game.c
138
src/in_game.c
@@ -11,15 +11,24 @@
|
||||
#include "constants.h"
|
||||
#include "in_game.h"
|
||||
|
||||
static const int I_SIZE = 257;
|
||||
typedef struct PLAYER {
|
||||
unsigned short x;
|
||||
unsigned short y;
|
||||
} player_t;
|
||||
|
||||
static const int I_SIZE = 513;
|
||||
static int ** imap;
|
||||
static bool ** wmap;
|
||||
static bool w_mov = FALSE;
|
||||
static bool uK, dK, lK, rK;
|
||||
static clock_t then;
|
||||
static player_t player;
|
||||
|
||||
void input();
|
||||
gsname_t update();
|
||||
void render(int, int);
|
||||
void drawGui(int, int);
|
||||
void setPlayerStart();
|
||||
|
||||
void initInGameState( gs_t * gs) {
|
||||
int n, i, j;
|
||||
@@ -64,6 +73,9 @@ void initInGameState( gs_t * gs) {
|
||||
free(map[ i ]);
|
||||
}
|
||||
free(map);
|
||||
|
||||
setPlayerStart();
|
||||
uK = dK = lK = rK = FALSE;
|
||||
}
|
||||
|
||||
void input(){
|
||||
@@ -73,16 +85,40 @@ void input(){
|
||||
|
||||
if(key != ERR){
|
||||
fprintf(stderr, "\t%s: Caught keycode %d\n", __FILE__, key);
|
||||
if(key == KEY_UP) uK = TRUE;
|
||||
if(key == KEY_DOWN) dK = TRUE;
|
||||
if(key == KEY_LEFT) lK = TRUE;
|
||||
if(key == KEY_RIGHT) rK = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
gsname_t update(){
|
||||
if(uK){
|
||||
if(terrainType( imap[player.x][player.y - 1] ) != DEEP_WATER && terrainType( imap[player.x][player.y - 1] ) != MOUNTAIN) player.y -= 1;
|
||||
uK = FALSE;
|
||||
}
|
||||
|
||||
if(dK){
|
||||
if(terrainType( imap[player.x][player.y + 1]) != DEEP_WATER && terrainType( imap[player.x][player.y + 1]) != MOUNTAIN ) player.y += 1;
|
||||
dK = FALSE;
|
||||
}
|
||||
|
||||
if(lK){
|
||||
if(terrainType( imap[player.x - 1][player.y]) != DEEP_WATER && terrainType( imap[player.x - 1][player.y]) != MOUNTAIN) player.x -= 1;
|
||||
lK = FALSE;
|
||||
}
|
||||
|
||||
if(rK){
|
||||
if(terrainType( imap[player.x + 1][player.y]) != DEEP_WATER && terrainType( imap[player.x + 1][player.y]) != MOUNTAIN) player.x += 1;
|
||||
rK = FALSE;
|
||||
}
|
||||
|
||||
return IN_GAME;
|
||||
}
|
||||
|
||||
void render(int w, int h){
|
||||
clock_t now, delta;
|
||||
int i, j;
|
||||
int i, j, pi, pj, ioff, joff, di, dj;
|
||||
|
||||
now = clock();
|
||||
delta = now - then;
|
||||
@@ -91,16 +127,28 @@ void render(int w, int h){
|
||||
w_mov = TRUE;
|
||||
}
|
||||
|
||||
for(i = 0; i < w; i++){
|
||||
for(j = 0; j < h; j++){
|
||||
pi = (((w - 1) - 27) / 2) + 27;
|
||||
pj = (h - 2) / 2 + 1;
|
||||
|
||||
ioff = (w - 28) / 2;
|
||||
joff = (h - 2) / 2;
|
||||
|
||||
for(i = 27; i < w - 1; i++){
|
||||
for(j = 1; j < h - 1; j++){
|
||||
move(j, i);
|
||||
|
||||
switch(terrainType( imap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE] )){
|
||||
di = i - 27 + player.x - ioff;
|
||||
dj = j - 1 + player.y - joff;
|
||||
|
||||
if( di < 0 || di >= I_SIZE){
|
||||
printw(" ");
|
||||
}else{
|
||||
switch(terrainType( imap[di][dj] )){
|
||||
case DEEP_WATER:
|
||||
attron(COLOR_PAIR(DW_COLOR));
|
||||
if(w_mov)
|
||||
wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE] = !wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE];
|
||||
if(wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE])
|
||||
wmap[di][dj] = !wmap[di][dj];
|
||||
if(wmap[di][dj])
|
||||
printw("\u2248");
|
||||
else
|
||||
printw("~");
|
||||
@@ -108,8 +156,8 @@ void render(int w, int h){
|
||||
case SHALLOW_WATER:
|
||||
attron(COLOR_PAIR(SW_COLOR));
|
||||
if(w_mov)
|
||||
wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE] = !wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE];
|
||||
if(wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE])
|
||||
wmap[di][dj] = !wmap[di][dj];
|
||||
if(wmap[di][dj])
|
||||
printw("\u2248");
|
||||
else
|
||||
printw("~");
|
||||
@@ -135,8 +183,78 @@ void render(int w, int h){
|
||||
printw("\u25B2");
|
||||
break;
|
||||
}
|
||||
//printw("\u2588");
|
||||
}
|
||||
}
|
||||
}
|
||||
w_mov = FALSE;
|
||||
|
||||
move(pj, pi);
|
||||
attron(COLOR_PAIR(BSC_COLOR));
|
||||
printw(/*"\u263A"*/ "@");
|
||||
|
||||
drawGui(w, h);
|
||||
}
|
||||
|
||||
void drawGui(int w, int h){
|
||||
int i, j;
|
||||
|
||||
attron(COLOR_PAIR(BSC_COLOR));
|
||||
|
||||
/* Clear the gui space. */
|
||||
for(i = 1; i < 26; i++){
|
||||
for(j = 1; j < h - 1; j++){
|
||||
move(j, i);
|
||||
printw(" ");
|
||||
}
|
||||
}
|
||||
|
||||
/* Upper horizontal bar. */
|
||||
move(0, 0);
|
||||
printw("\u2554");
|
||||
for(i = 0; i < w - 2; i++){
|
||||
if(i != 25){
|
||||
printw("\u2550");
|
||||
}else{
|
||||
printw("\u2566");
|
||||
}
|
||||
}
|
||||
printw("\u2557");
|
||||
|
||||
/* Lower horizontal bar. */
|
||||
move(h - 1, 0);
|
||||
printw("\u255A");
|
||||
for(i = 0; i < w - 2; i++){
|
||||
if(i != 25){
|
||||
printw("\u2550");
|
||||
}else{
|
||||
printw("\u2569");
|
||||
}
|
||||
}
|
||||
printw("\u255D");
|
||||
|
||||
/* Vertical bars. */
|
||||
for(i = 1; i < h - 1; i++){
|
||||
move(i, 0);
|
||||
printw("\u2551");
|
||||
move(i, 26);
|
||||
printw("\u2551");
|
||||
move(i, w-1);
|
||||
printw("\u2551");
|
||||
}
|
||||
}
|
||||
|
||||
void setPlayerStart(){
|
||||
int x, y;
|
||||
bool posFound = false;
|
||||
|
||||
while(!posFound){
|
||||
x = (I_SIZE / 4) + (rand() % (I_SIZE / 2));
|
||||
y = (I_SIZE / 4) + (rand() % (I_SIZE / 2));
|
||||
|
||||
if(terrainType(imap[x][y]) == GRASS){
|
||||
player.x = x;
|
||||
player.y = y;
|
||||
posFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -132,8 +132,8 @@ int main() {
|
||||
fps = 0;
|
||||
}
|
||||
|
||||
move(0, 0);
|
||||
attron(COLOR_PAIR(BAR_COLOR));
|
||||
move(1, 1);
|
||||
attron(COLOR_PAIR(BSC_COLOR));
|
||||
printw("FPS: %u", pfps);
|
||||
|
||||
refresh();
|
||||
|
Reference in New Issue
Block a user