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 "constants.h"
|
||||||
#include "in_game.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 int ** imap;
|
||||||
static bool ** wmap;
|
static bool ** wmap;
|
||||||
static bool w_mov = FALSE;
|
static bool w_mov = FALSE;
|
||||||
|
static bool uK, dK, lK, rK;
|
||||||
static clock_t then;
|
static clock_t then;
|
||||||
|
static player_t player;
|
||||||
|
|
||||||
void input();
|
void input();
|
||||||
gsname_t update();
|
gsname_t update();
|
||||||
void render(int, int);
|
void render(int, int);
|
||||||
|
void drawGui(int, int);
|
||||||
|
void setPlayerStart();
|
||||||
|
|
||||||
void initInGameState( gs_t * gs) {
|
void initInGameState( gs_t * gs) {
|
||||||
int n, i, j;
|
int n, i, j;
|
||||||
@@ -64,6 +73,9 @@ void initInGameState( gs_t * gs) {
|
|||||||
free(map[ i ]);
|
free(map[ i ]);
|
||||||
}
|
}
|
||||||
free(map);
|
free(map);
|
||||||
|
|
||||||
|
setPlayerStart();
|
||||||
|
uK = dK = lK = rK = FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void input(){
|
void input(){
|
||||||
@@ -73,16 +85,40 @@ void input(){
|
|||||||
|
|
||||||
if(key != ERR){
|
if(key != ERR){
|
||||||
fprintf(stderr, "\t%s: Caught keycode %d\n", __FILE__, key);
|
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(){
|
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;
|
return IN_GAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(int w, int h){
|
void render(int w, int h){
|
||||||
clock_t now, delta;
|
clock_t now, delta;
|
||||||
int i, j;
|
int i, j, pi, pj, ioff, joff, di, dj;
|
||||||
|
|
||||||
now = clock();
|
now = clock();
|
||||||
delta = now - then;
|
delta = now - then;
|
||||||
@@ -91,16 +127,28 @@ void render(int w, int h){
|
|||||||
w_mov = TRUE;
|
w_mov = TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i = 0; i < w; i++){
|
pi = (((w - 1) - 27) / 2) + 27;
|
||||||
for(j = 0; j < h; j++){
|
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);
|
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:
|
case DEEP_WATER:
|
||||||
attron(COLOR_PAIR(DW_COLOR));
|
attron(COLOR_PAIR(DW_COLOR));
|
||||||
if(w_mov)
|
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];
|
wmap[di][dj] = !wmap[di][dj];
|
||||||
if(wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE])
|
if(wmap[di][dj])
|
||||||
printw("\u2248");
|
printw("\u2248");
|
||||||
else
|
else
|
||||||
printw("~");
|
printw("~");
|
||||||
@@ -108,8 +156,8 @@ void render(int w, int h){
|
|||||||
case SHALLOW_WATER:
|
case SHALLOW_WATER:
|
||||||
attron(COLOR_PAIR(SW_COLOR));
|
attron(COLOR_PAIR(SW_COLOR));
|
||||||
if(w_mov)
|
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];
|
wmap[di][dj] = !wmap[di][dj];
|
||||||
if(wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE])
|
if(wmap[di][dj])
|
||||||
printw("\u2248");
|
printw("\u2248");
|
||||||
else
|
else
|
||||||
printw("~");
|
printw("~");
|
||||||
@@ -135,8 +183,78 @@ void render(int w, int h){
|
|||||||
printw("\u25B2");
|
printw("\u25B2");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//printw("\u2588");
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w_mov = FALSE;
|
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;
|
fps = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
move(0, 0);
|
move(1, 1);
|
||||||
attron(COLOR_PAIR(BAR_COLOR));
|
attron(COLOR_PAIR(BSC_COLOR));
|
||||||
printw("FPS: %u", pfps);
|
printw("FPS: %u", pfps);
|
||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
|
Reference in New Issue
Block a user