Browse Source

Movement and stats

master
Robert Newman 5 years ago
parent
commit
997e2f5f2f
  1. 85
      src/devrogue.c

85
src/devrogue.c

@ -16,6 +16,8 @@ static struct class *devclass = NULL;
static struct cdev devcdev;
static struct device *devdevice = NULL;
static void rogue_draw_stat(void);
static void rogue_update_state(char);
static int rogue_open(struct inode *, struct file *);
static int rogue_release(struct inode *, struct file *);
static ssize_t rogue_read(struct file *, char *, size_t, loff_t *);
@ -24,9 +26,27 @@ static ssize_t rogue_write(struct file *, const char *, size_t, loff_t *);
static int count_act = 0;
static char shouldDisplay = 1;
static char actionqueue[] = "blahblahblah";
static char gamebuffer[(80+1)*24+1];
#define PLAYFIELD_WIDTH (80+1)
#define PLAYFIELD_HEIGHT 24
// 8+2+4+16+3+4+7+2+4+30 = 80
static char stat_fmt[] = "Health: %2d Enemies Killed: %3d Player Level: %2d Floor: %2d %10s\n";
static char statbar[82];
static char gamebuffer[PLAYFIELD_WIDTH*PLAYFIELD_HEIGHT+1];
static int playerPos = 1+PLAYFIELD_WIDTH; // Start the player at coordinate 1,1 where 0,0 is the top left
static int enemyPos[30];
#define PLAYER_HEALTH_MAX 99
#define LEVEL_MAX 99
#define ENEMIES_MAX 600
static int playerHealth = 20; // Cap at 99
static int enemiesKilled = 0; // Cap at 600
static int playerLevel = 1; // Cap at 99
static int currentFloor = 1; // Cap at 99
#define len_queue ((sizeof(actionqueue)/sizeof(actionqueue[0])) - 1)
@ -38,30 +58,41 @@ static struct file_operations dev_ops = {
.release = rogue_release
};
static void rogue_draw_stat() {
int i = 0;
sprintf(statbar, stat_fmt, playerHealth, enemiesKilled, playerLevel, currentFloor, "");
for(i = 0; i<PLAYFIELD_WIDTH; i++) {
gamebuffer[i+(PLAYFIELD_HEIGHT-1)*PLAYFIELD_WIDTH] = statbar[i];
}
}
int init_module() {
int status;
// Draw basic fullscreen room
int y = 22;
int x = 78;
int y = (PLAYFIELD_HEIGHT-3);
int x = (PLAYFIELD_WIDTH-3);
while(y-- > 1) {
x = 78;
gamebuffer[0+81*y] = '*';
x = (PLAYFIELD_WIDTH-3);
gamebuffer[0+PLAYFIELD_WIDTH*y] = '*';
while(x-- > 1) {
gamebuffer[x+81*y] = '.';
gamebuffer[x+PLAYFIELD_WIDTH*y] = '.';
}
gamebuffer[79+81*y] = '*';
gamebuffer[80+81*y] = '\n';
gamebuffer[(PLAYFIELD_WIDTH-2)+PLAYFIELD_WIDTH*y] = '*';
gamebuffer[(PLAYFIELD_WIDTH-1)+PLAYFIELD_WIDTH*y] = '\n';
}
x = 79;
x = (PLAYFIELD_WIDTH-2);
while(x-- > 0)
{
gamebuffer[x+0*81] = '*';
gamebuffer[x+23*81] = '*';
gamebuffer[x+0*PLAYFIELD_WIDTH] = '*';
gamebuffer[x+(PLAYFIELD_HEIGHT-2)*PLAYFIELD_WIDTH] = '*';
}
gamebuffer[80+0*81] = '\n';
gamebuffer[80+23*81] = '\n';
gamebuffer[(PLAYFIELD_WIDTH-1)+0*PLAYFIELD_WIDTH] = '\n';
gamebuffer[(PLAYFIELD_WIDTH-1)+(PLAYFIELD_HEIGHT-2)*PLAYFIELD_WIDTH] = '\n';
gamebuffer[sizeof(gamebuffer)-1] = '\0';
gamebuffer[playerPos] = '@';
rogue_draw_stat();
if((status = alloc_chrdev_region(&devnode, 0, 1, rogue)) < 0) {
printk(KERN_ALERT "Can't rogue: %d\n", status);
@ -104,7 +135,33 @@ static int rogue_release(struct inode *ino, struct file *fil) {
}
static void rogue_update_state(char action) {
gamebuffer[playerPos] = '.';
switch(action) {
case 'u':
if(playerPos - PLAYFIELD_WIDTH >= 0)
if((gamebuffer[playerPos-PLAYFIELD_WIDTH] == '.'))
playerPos -= PLAYFIELD_WIDTH;
break;
case 'd':
if((playerPos + PLAYFIELD_WIDTH)/PLAYFIELD_WIDTH < (PLAYFIELD_HEIGHT-1))
if(gamebuffer[playerPos+PLAYFIELD_WIDTH] == '.')
playerPos += PLAYFIELD_WIDTH;
break;
case 'l':
if((playerPos%PLAYFIELD_WIDTH)-1 >= 0)
if((gamebuffer[playerPos-1] == '.'))
playerPos -= 1;
break;
case 'r':
if((playerPos%PLAYFIELD_WIDTH)+1 < PLAYFIELD_WIDTH)
if((gamebuffer[playerPos+1] == '.'))
playerPos += 1;
break;
}
if(playerHealth < PLAYER_HEALTH_MAX)
playerHealth += 1;
rogue_draw_stat();
gamebuffer[playerPos] = '@';
}
static ssize_t rogue_read(struct file *fil, char *buf, size_t len, loff_t *off) {

Loading…
Cancel
Save