Skip to content

Instantly share code, notes, and snippets.

@Pharap

Pharap/Entity.h Secret

Created November 6, 2018 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pharap/a55e448de9e8da4608af25d28fa24d3a to your computer and use it in GitHub Desktop.
Save Pharap/a55e448de9e8da4608af25d28fa24d3a to your computer and use it in GitHub Desktop.
A slightly modified version of SamSibbens's game.
#pragma once
#include <Arduboy2.h>
#include "Images.h"
#include "World.h"
constexpr uint8_t EntityWidth = 8;
enum class EntityType : uint8_t {
None,
Player,
Knight,
Sword,
};
enum class Direction : uint8_t {
Down,
Right,
Left,
Up,
};
struct Entity {
int8_t x = 0;
int8_t y = 0;
EntityType type = EntityType::None;
uint8_t damage_taken = 0;
uint8_t state = 0;
Direction facing = Direction::Down;
};
extern Arduboy2 arduboy;
inline void draw_entity(Entity& ent) {
uint8_t x = ent.x;
uint8_t y = ent.y;
switch(ent.type){
case EntityType::Player:
arduboy.drawBitmap(x, y, spr_human_outline, 8, 8, BLACK);
arduboy.drawBitmap(x, y, spr_human, 8, 8, WHITE);
break;
default:
break;
}
}
inline bool detect_wall(int8_t x, int8_t y) {
int8_t x1 = x_on_grid(x);
int8_t x2 = x_on_grid(x+TILE_WIDTH-1);
int8_t y1 = y_on_grid(y);
int8_t y2 = y_on_grid(y+TILE_WIDTH-1);
return ((is_tile_solid(x1, y1) || is_tile_solid(x2, y1) || is_tile_solid(x2, y2) || is_tile_solid(x1, y2)));
}
inline void draw_walls() {
for(uint8_t x = 0; x < WORLD_WIDTH; x++) {
for(uint8_t y = 0; y < WORLD_HEIGHT; y++) {
if (is_tile_solid(x, y)) {
//arduboy.drawRect(x*TILE_WIDTH, y*TILE_WIDTH, 8, 8, WHITE);
arduboy.drawBitmap(x*TILE_WIDTH, y*TILE_WIDTH, spr_wall, 8, 8, BLACK);
}
}
}
}
inline void move_entity(Entity& ent, int8_t xmove, int8_t ymove) {
int8_t xnew = ent.x + xmove;
int8_t ynew = ent.y + ymove;
if(!detect_wall(xnew, ent.y)) {
ent.x = xnew;
}
if(!detect_wall(ent.x, ynew)) {
ent.y = ynew;
}
}
inline void update_entity(Entity& ent) {
switch(ent.type){
case EntityType::Player:
{
int8_t xinput = 0;
int8_t yinput = 0;
if(arduboy.pressed(LEFT_BUTTON)) {xinput--;}
if(arduboy.pressed(RIGHT_BUTTON)) {xinput++;}
if(arduboy.pressed(UP_BUTTON)) {yinput--;}
if(arduboy.pressed(DOWN_BUTTON)) {yinput++;}
move_entity(ent, xinput, yinput);
}
break;
default:
break;
}
}
#pragma once
/*** HUMANOID SPRITES ***/
// Humanoid (general sprites) //
const unsigned char spr_human_outline[] PROGMEM = {
0x3f, 0xe1, 0xad, 0xa1, 0xa1, 0xad, 0xe1, 0x3f,
};
const unsigned char spr_human[] PROGMEM = {
0x00, 0x1e, 0x5e, 0x5e, 0x5e, 0x5e, 0x1e, 0x00,
};
// Player Faces //
const unsigned char spr_player_down[] PROGMEM = {
0x3f, 0xe1, 0xad, 0xa1, 0xa1, 0xad, 0xe1, 0x3f,
};
const unsigned char spr_player_right[] PROGMEM = {
0x00, 0x1e, 0x5e, 0x52, 0x5e, 0x52, 0x1e, 0x00,
};
const unsigned char spr_player_left[] PROGMEM = {
0x00, 0x1e, 0x52, 0x5e, 0x52, 0x5e, 0x1e, 0x00,
};
/*** WORLD SPRITES ***/
const unsigned char spr_wall[] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
// Sam Sibbens
// 2018 NOVEMBER 01
// Unnamed project
#include <Arduino.h>
#include <Arduboy2.h>
Arduboy2 arduboy;
#include "World.h"
#include "Images.h"
#include "Entity.h"
constexpr uint8_t MAX_ENTITY_COUNT = 10;
Entity entities[MAX_ENTITY_COUNT];
Entity& player = entities[0];
Entity& knight = entities[1];
/* FUNCTIONS DECLARED AND DEFINED IN THIS FILE */
void draw_entities();
void setup() {
arduboy.begin();
arduboy.clear();
arduboy.setFrameRate(30);
arduboy.initRandomSeed();
player.type = EntityType::Player;
player.x = 64 - 4;
player.y = 16 - 4;
for(uint8_t x = 0; x < WORLD_WIDTH; x++) {
for(uint8_t y = 0; y < WORLD_HEIGHT; y++) {
// clear the level
level[x][y] = false;
// make borders solid
if (x == 0 || x == WORLD_WIDTH-1 || y == 0 || y == WORLD_HEIGHT-1) {
level[x][y] = true;
continue;
}
// randomly make some tiles solid
if ((rand() % 5) <= 0) {level[x][y] = true;}
}
}
}
void loop() {
//Prevent the Arduboy from running too fast
if(!arduboy.nextFrame()) {return;}
arduboy.clear();
arduboy.fillScreen(WHITE);
draw_walls();
update_entities();
arduboy.display();
}
/**** FUNCTIONS ****/
void update_entities() {
for(uint8_t i = 0; i < MAX_ENTITY_COUNT; i++) {
update_entity(entities[i]);
draw_entity(entities[i]);
}
}
#pragma once
constexpr uint8_t WORLD_WIDTH = 16;
constexpr uint8_t WORLD_HEIGHT = 8;
constexpr uint8_t TILE_WIDTH = 8;
bool level[WORLD_WIDTH][WORLD_HEIGHT];
inline int8_t x_on_grid(int16_t x) {
x = (x / TILE_WIDTH);
x = max(x, 0);
x = min(x, WORLD_WIDTH-1);
return x;
}
inline int8_t y_on_grid(int16_t y) {
y = (y / TILE_WIDTH);
y = max(y, 0);
y = min(y, WORLD_HEIGHT-1);
return y;
}
inline bool is_tile_solid(int16_t x, int16_t y) {
return level[x][y];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment