-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds collision detection/response demos
- Loading branch information
Showing
11 changed files
with
576 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "Player.h" | ||
|
||
Player::Player(uint16_t x, uint16_t y) | ||
: _x(x) | ||
, _y(y) | ||
, _vx(0) | ||
, _vy(0) | ||
{} | ||
|
||
int16_t Player::x() { return _x; } | ||
int16_t Player::y() { return _y; } | ||
int16_t Player::right() { return _x + SIZE - 1; } | ||
int16_t Player::bottom() { return _y + SIZE - 1; } | ||
|
||
void Player::moveLeft() { _vx = -SPEED; } | ||
void Player::moveRight() { _vx = SPEED; } | ||
void Player::moveUp() { _vy = -SPEED; } | ||
void Player::moveDown() { _vy = SPEED; } | ||
|
||
void Player::_handleHorizontalCollisions(Tilemap &tilemap) { | ||
|
||
// hit points along the y-axis | ||
uint16_t hpy[HIT_POINTS]; | ||
|
||
hpy[0] = _y; | ||
hpy[HIT_POINTS - 1] = bottom(); | ||
if (HIT_POINTS > 2) for (uint8_t i = 1; i < HIT_POINTS - 1; ++i) hpy[i] = _y + i * Tilemap::TILE_SIZE; | ||
|
||
uint16_t tx = (_vx < 0 ? _x : right()) / Tilemap::TILE_SIZE; | ||
|
||
for (uint8_t i = 0; i < HIT_POINTS; ++i) { | ||
|
||
uint16_t ty = hpy[i] / Tilemap::TILE_SIZE; | ||
|
||
if (tilemap.isSolid(tx, ty)) { | ||
_x = _vx < 0 ? (tx + 1) * Tilemap::TILE_SIZE : tx * Tilemap::TILE_SIZE - SIZE; | ||
return; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
void Player::_handleVerticalCollisions(Tilemap &tilemap) { | ||
|
||
// hit points along the x-axis | ||
uint16_t hpx[HIT_POINTS]; | ||
|
||
hpx[0] = _x; | ||
hpx[HIT_POINTS - 1] = right(); | ||
if (HIT_POINTS > 2) for (uint8_t i = 1; i < HIT_POINTS - 1; ++i) hpx[i] = _x + i * Tilemap::TILE_SIZE; | ||
|
||
uint16_t ty = (_vy < 0 ? _y : bottom()) / Tilemap::TILE_SIZE; | ||
|
||
for (uint8_t i = 0; i < HIT_POINTS; ++i) { | ||
|
||
uint16_t tx = hpx[i] / Tilemap::TILE_SIZE; | ||
|
||
if (tilemap.isSolid(tx, ty)) { | ||
_y = _vy < 0 ? (ty + 1) * Tilemap::TILE_SIZE : ty * Tilemap::TILE_SIZE - SIZE; | ||
return; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
void Player::update(Tilemap &tilemap) { | ||
|
||
if (_vx) { | ||
|
||
_x += _vx; _handleHorizontalCollisions(tilemap); | ||
_vx *= .8f; if (abs(_vx) < .2f) _vx = 0; | ||
|
||
} | ||
|
||
if (_vy) { | ||
|
||
_y += _vy; _handleVerticalCollisions(tilemap); | ||
_vy *= .8f; if (abs(_vy) < .2f) _vy = 0; | ||
|
||
} | ||
|
||
} | ||
|
||
void Player::draw(LGFX_Sprite &fb) { | ||
|
||
fb.fillRect(_x, _y, SIZE, SIZE, 0xff60); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
|
||
#include <ESPboy.h> | ||
#include "Tilemap.h" | ||
|
||
class Player { | ||
|
||
private: | ||
|
||
static constexpr uint8_t SIZE = 16; | ||
static constexpr uint8_t HIT_POINTS = 1 + SIZE / Tilemap::TILE_SIZE; | ||
|
||
static constexpr uint8_t SPEED = 2; | ||
|
||
|
||
float_t _x, _y; | ||
float_t _vx, _vy; | ||
|
||
void _handleHorizontalCollisions(Tilemap &tilemap); | ||
void _handleVerticalCollisions(Tilemap &tilemap); | ||
|
||
public: | ||
|
||
Player(uint16_t x, uint16_t y); | ||
|
||
int16_t x(); | ||
int16_t y(); | ||
int16_t right(); | ||
int16_t bottom(); | ||
|
||
void moveLeft(); | ||
void moveRight(); | ||
void moveUp(); | ||
void moveDown(); | ||
|
||
void update(Tilemap &tilemap); | ||
void draw(LGFX_Sprite &fb); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "Tilemap.h" | ||
|
||
bool Tilemap::isSolid(uint8_t x, uint8_t y) { | ||
|
||
return pgm_read_word(TILE + y) & (0x8000 >> x); | ||
|
||
} | ||
|
||
void Tilemap::draw(LGFX_Sprite &fb) { | ||
|
||
for (uint8_t ty = 0; ty < HEIGHT; ++ty) { | ||
for (uint8_t tx = 0; tx < WIDTH; ++tx) { | ||
|
||
if (isSolid(tx, ty)) | ||
fb.fillRect(tx * TILE_SIZE, ty * TILE_SIZE, TILE_SIZE, TILE_SIZE, 0x34df); | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <ESPboy.h> | ||
|
||
class Tilemap { | ||
|
||
private: | ||
|
||
static constexpr uint16_t TILE[] PROGMEM = { | ||
|
||
0b1111111111111111, | ||
0b1000000000000001, | ||
0b1000000000000001, | ||
0b1000100000010001, | ||
0b1000111000011001, | ||
0b1000100000000001, | ||
0b1000000000000001, | ||
0b1000000000011111, | ||
0b1111000000000001, | ||
0b1000000110000001, | ||
0b1000000110000001, | ||
0b1000011111100001, | ||
0b1000011111100001, | ||
0b1000000000000001, | ||
0b1000000000000001, | ||
0b1111111111111111 | ||
|
||
}; | ||
|
||
public: | ||
|
||
static constexpr uint8_t TILE_SIZE = 8; | ||
static constexpr uint8_t WIDTH = TFT_WIDTH / TILE_SIZE; | ||
static constexpr uint8_t HEIGHT = TFT_HEIGHT / TILE_SIZE; | ||
|
||
bool isSolid(uint8_t x, uint8_t y); | ||
|
||
void draw(LGFX_Sprite &fb); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <ESPboy.h> | ||
#include "Tilemap.h" | ||
#include "Player.h" | ||
|
||
#define SHOW_FPS 0 | ||
|
||
ESPboy espboy; | ||
LGFX_Sprite fb(&espboy.tft); | ||
Tilemap tilemap; | ||
Player player(96, 72); | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Initialization | ||
// ---------------------------------------------------------------------------- | ||
|
||
void setup() { | ||
|
||
espboy.begin(false); | ||
fb.createSprite(TFT_WIDTH, TFT_HEIGHT); | ||
|
||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// User controls | ||
// ---------------------------------------------------------------------------- | ||
|
||
void input() { | ||
|
||
uint8_t read = espboy.readButtons(); | ||
|
||
if (read & 0x01) player.moveLeft(); | ||
else if (read & 0x08) player.moveRight(); | ||
|
||
if (read & 0x02) player.moveUp(); | ||
else if (read & 0x04) player.moveDown(); | ||
|
||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Game handling | ||
// ---------------------------------------------------------------------------- | ||
|
||
void update() { | ||
|
||
player.update(tilemap); | ||
|
||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Graphics rendering | ||
// ---------------------------------------------------------------------------- | ||
|
||
void draw() { | ||
|
||
fb.fillSprite(0); | ||
|
||
tilemap.draw(fb); | ||
player.draw(fb); | ||
|
||
#if SHOW_FPS | ||
fb.setTextColor(0); | ||
fb.drawNumber(espboy.fps(), 1, 2); | ||
fb.setTextColor(0xffff); | ||
fb.drawNumber(espboy.fps(), 1, 1); | ||
#endif | ||
|
||
fb.pushSprite(0, 0); | ||
|
||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Main control loop | ||
// ---------------------------------------------------------------------------- | ||
|
||
void loop() { | ||
|
||
espboy.update(); | ||
|
||
input(); | ||
update(); | ||
draw(); | ||
|
||
} |
Oops, something went wrong.