Entities

Understanding the language, error messages, etc.

Entities

Postby awesome101 » Wed Jun 24, 2015 7:09 pm

Hey guys, I'm working on a mario platformer, and I want to add enemies to my game.
I have the Entity class all done, but I want to know how to spawn them.
In the main ino file, you can see a comment that says: "// spawn goomba here" That is where I want to spawn the goomba.
Here is my code so far:

main ino file:
Code: Select all
#include <SPI.h>
#include <Gamebuino.h>

#include "mario_bitmaps.h"
#include "player.h"
#include "Entity.h"

#define MAX_LEVEL_WIDTH  30
#define MAX_LEVEL_HEIGHT 6

extern const byte PROGMEM levels[];
extern Entity entities[];
extern byte velX;
extern byte velY;

const byte *block = NULL;
int cameraPos = 0;
byte lvldata[MAX_LEVEL_WIDTH * MAX_LEVEL_HEIGHT];
byte lvl = 1;
Gamebuino gb;
Player player(0, LCDHEIGHT - 24);

byte i = 0, j = 0; // for loop variables

void loadLevel(int curLev) {
  for(i = 0; i<MAX_LEVEL_WIDTH * MAX_LEVEL_HEIGHT; i++){//Loops through the level
    lvldata[i] = pgm_read_byte(levels + (MAX_LEVEL_WIDTH * MAX_LEVEL_HEIGHT) * (curLev - 1) + i);
  }
}

void renderLevel() {
  cameraPos = player.x * 2;
  for(i = 0;i < MAX_LEVEL_HEIGHT;i++) {
    for(j = 0;j < MAX_LEVEL_WIDTH;j++) {
      switch(lvldata[j + i * MAX_LEVEL_WIDTH]) {
        case 0:
        block = empty;
        break;
        case 1:
        block = sand_ground;
        break;
        case 2:
        block = diagonal_right;
        break;
        case 3:
        block = diagonal_left;
        break;
        case 4:
        block = NULL;
        // spawn goomba here
        break;
        default:
        break;
      }
      if(block != NULL) {
        if((j << 3) - cameraPos >= -8 && (j << 3) - cameraPos < LCDWIDTH &&
        (i << 3) > 0 && (i << 3) < LCDHEIGHT) {
          gb.display.drawBitmap((j << 3) - cameraPos, i << 3, block);
        }
      }
    }
  }
}

void setup() {
  gb.begin();
  gb.titleScreen(F("Super Mario Land"), title);
  loadLevel(lvl);
}

void loop() {
  if(gb.update()) {
    gb.display.clear();
    renderLevel();
    player.render(gb);
    renderEntities(gb);
    if(gb.buttons.pressed(BTN_C)) {
        gb.titleScreen(F("Super Mario land"), title);
        loadLevel(1);
    }
  }
}


Entity:
Code: Select all
#ifndef ENTITY_H
#define ENTITY_H

#define MAX_NUM_OF_ENTITIES 2

class Entity {
  public:
  Entity() {
    this->x = 0;
    this->y = 0;
    this->velX = 0;
    this->velY = 0;
    this->bitMap = NULL;
    this->flip = false;
    this->walking = false;
  }
 
  float x, y;
  float velX, velY;
  const byte *bitMap;
  boolean flip;
  boolean walking;
 
  void render(Gamebuino gb) {
    x += velX;
    y += velY;
 
    if(velX < 0) flip = true;
    else flip = false;
   
    if(flip) gb.display.drawBitmap(x, y, this->bitMap, NOROT, FLIPH);
    else gb.display.drawBitmap(x, y, this->bitMap, NOROT, NOFLIP);
  }
};

Entity entities[MAX_NUM_OF_ENTITIES];

void addEntity(int x, int y, const byte *b) {
  for(byte i = 0;i < MAX_NUM_OF_ENTITIES;i++) {
    if(entities[i].bitMap == NULL) {
      entities[i] = Entity();
      entities[i].bitMap = b;
      entities[i].x = x;
      entities[i].y = y;
    }
  }
}

void renderEntities(Gamebuino gb) {
  for(byte i = 0;i < MAX_NUM_OF_ENTITIES;i++) {
    if(entities[i].bitMap != NULL) {
      entities[i].render(gb);
    } else {
      break;
    }
  }
}

#endif



MarioBitmaps:
Code: Select all
#ifndef MARIO_BITMAPS_H
#define MARIO_BITMAPS_H

byte mario_still[] PROGMEM = {16,16,
0x1F,0x0,
0x20,0xC0,
0x4F,0x20,
0x72,0xC0,
0xB2,0x20,
0xB1,0x20,
0x63,0xC0,
0x30,0x80,
0x4F,0x0,
0x86,0x80,
0xBF,0x80,
0x4F,0x80,
0x53,0x80,
0x21,0x0,
0x3E,0x0,
0x3E,0x0,
};

byte mario_walking_1[] PROGMEM = {16,16,
0x1F,0x0,
0x20,0xC0,
0x4F,0x20,
0x72,0xC0,
0xB2,0x20,
0xB1,0x20,
0x63,0xC0,
0x30,0x80,
0x4F,0x0,
0x86,0x80,
0xBF,0x80,
0x4F,0x80,
0x5B,0x80,
0xBE,0x40,
0x96,0x40,
0x61,0x80,
};

byte sand_ground[] PROGMEM = {8,8,
0xFF,
0x0,
0xFF,
0xFF,
0x10,
0x0,
0x80,
0x21,
};

const byte diagonal_right[] PROGMEM = {8,8,
0x80,
0x40,
0x20,
0x10,
0x8,
0x4,
0x2,
0x1,
};

const byte diagonal_left[] PROGMEM = {8,8,
0x1,
0x2,
0x4,
0x8,
0x10,
0x20,
0x40,
0x80,
};

const byte empty[] PROGMEM = {8,8,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
};

const byte goomba[] PROGMEM = {16,9,
0x3E,0x0,
0x7F,0x0,
0xDD,0x80,
0xDD,0x80,
0xFF,0x80,
0x3E,0x0,
0x3E,0x0,
0x7F,0x0,
0x66,0x0,
};

const byte title[] PROGMEM = {64,30,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0xF,0x80,0x1,0x80,0x0,
0x0,0x0,0x0,0x10,0x60,0x2,0x40,0x0,
0x0,0x0,0x0,0x27,0x90,0x4,0x20,0x0,
0x0,0x0,0x0,0x39,0x60,0x8,0x10,0x0,
0x0,0x0,0x0,0x59,0x10,0x10,0x8,0x0,
0x0,0x0,0x0,0x58,0x90,0x20,0x4,0x0,
0x0,0x0,0x0,0x31,0xE0,0x40,0x2,0x0,
0x0,0x0,0x0,0x18,0x40,0x80,0x1,0x0,
0x1,0x80,0x0,0x27,0x81,0x0,0x0,0x80,
0x2,0x40,0x0,0x43,0x42,0x0,0x0,0x40,
0x4,0x20,0x0,0x5F,0xC4,0x0,0x0,0x20,
0x8,0x10,0x0,0x27,0xC8,0x0,0x0,0x10,
0x10,0x8,0x0,0x2D,0xD0,0x0,0x0,0x8,
0x20,0x4,0x0,0x5F,0x20,0x0,0x0,0x4,
0x40,0x2,0x0,0x4B,0x20,0x0,0x0,0x2,
0x80,0x1,0x0,0x30,0xC0,0x0,0x0,0x1,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,
};

#endif /* MARIO_BITMAPS_H */
awesome101
 
Posts: 159
Joined: Sat Jun 20, 2015 6:56 pm

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 70 guests

cron