|
|
|
#define OLC_PGE_APPLICATION
|
|
|
|
#include "pixelGameEngine.h"
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#define boolean char
|
|
|
|
#define true 1
|
|
|
|
#define false 0
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#define UP 0
|
|
|
|
#define RIGHT 1
|
|
|
|
#define DOWN 2
|
|
|
|
#define LEFT 3
|
|
|
|
|
|
|
|
#define TAIL_DISTANCE 4
|
|
|
|
|
|
|
|
struct TailData{
|
|
|
|
char direction = 0;
|
|
|
|
char*future_direction;
|
|
|
|
int*pos;
|
|
|
|
int*frameCount;
|
|
|
|
int waitTime = 0;
|
|
|
|
char futureIndex=0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MeercaChase : public olc::PixelGameEngine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
olc::Decal*meerca,*negg,*badnegg;
|
|
|
|
const float UPDATE_RATE = 0.016666667f;
|
|
|
|
const int NEGG_BOUNDARY=6;
|
|
|
|
float accumulatedTime = 0.0f;
|
|
|
|
int frameCount=0;
|
|
|
|
int moveSpd=4;
|
|
|
|
float meercaSpd[2] = {0,0};
|
|
|
|
float meercaPos[2] = {240,240};
|
|
|
|
float neggPos[2] = {};
|
|
|
|
float*badNeggs=NULL;
|
|
|
|
int badNeggCount=0;
|
|
|
|
TailData*tail=NULL;
|
|
|
|
int tailSize=0;
|
|
|
|
|
|
|
|
void assignNeggRandomPos() {
|
|
|
|
do {
|
|
|
|
neggPos[0]=(float)rand()/(float)RAND_MAX*(480-NEGG_BOUNDARY*2)+NEGG_BOUNDARY;
|
|
|
|
neggPos[1]=(float)rand()/(float)RAND_MAX*(480-NEGG_BOUNDARY*2)+NEGG_BOUNDARY;
|
|
|
|
} while(!positionFree(neggPos));
|
|
|
|
}
|
|
|
|
void spawnBadNegg() {
|
|
|
|
badNeggs=(float*)realloc(badNeggs,sizeof(float)*(++badNeggCount*2));
|
|
|
|
do {
|
|
|
|
badNeggs[badNeggCount*2-2] = (float)rand()/(float)RAND_MAX*(480-NEGG_BOUNDARY*2)+NEGG_BOUNDARY;
|
|
|
|
badNeggs[badNeggCount*2-1] = (float)rand()/(float)RAND_MAX*(480-NEGG_BOUNDARY*2)+NEGG_BOUNDARY;
|
|
|
|
} while(!positionFree(badNeggs+(badNeggCount*2-2),true));
|
|
|
|
}
|
|
|
|
boolean positionFree(float*pos) {
|
|
|
|
return positionFree(pos,false);
|
|
|
|
}
|
|
|
|
boolean positionFree(float*pos,boolean badNegg) {
|
|
|
|
if (badNegg&&abs(pos[0]-neggPos[0])<4&&abs(pos[1]-neggPos[1])<4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (abs(pos[0]-meercaPos[0])<64&&abs(pos[1]-meercaPos[1])<64) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int i=0;i<((badNegg)?badNeggCount-1:badNeggCount);i++) {
|
|
|
|
if (abs(pos[0]-badNeggs[i*2])<4&&abs(pos[1]-badNeggs[i*2+1])<4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
MeercaChase()
|
|
|
|
{
|
|
|
|
sAppName = "Meerca Chase";
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
void init() {
|
|
|
|
srand(432189);
|
|
|
|
assignNeggRandomPos();
|
|
|
|
if (badNeggs!=NULL) {free(badNeggs);}
|
|
|
|
if (tail!=NULL) {
|
|
|
|
for (int i=0;i<tailSize;i++) {
|
|
|
|
free(tail[i].future_direction);
|
|
|
|
free(tail[i].pos);
|
|
|
|
free(tail[i].frameCount);
|
|
|
|
}
|
|
|
|
free(tail);}
|
|
|
|
badNeggs=(float*)malloc(sizeof(float)*0);
|
|
|
|
tail=(TailData*)malloc(sizeof(TailData)*0);
|
|
|
|
badNeggCount=0;
|
|
|
|
tailSize=0;
|
|
|
|
meercaPos[0]=240;
|
|
|
|
meercaPos[1]=240;
|
|
|
|
meercaSpd[0]=0;
|
|
|
|
meercaSpd[1]=0;
|
|
|
|
}
|
|
|
|
bool OnUserCreate() override
|
|
|
|
{
|
|
|
|
meerca = new olc::Decal(new olc::Sprite("assets/meerca.png"));
|
|
|
|
negg = new olc::Decal(new olc::Sprite("assets/negg.png"));
|
|
|
|
badnegg = new olc::Decal(new olc::Sprite("assets/badnegg.png"));
|
|
|
|
SetPixelMode(olc::Pixel::ALPHA);
|
|
|
|
ConsoleCaptureStdOut(true);
|
|
|
|
Clear(olc::DARK_CYAN);
|
|
|
|
|
|
|
|
// Called once at the start, so create things here
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnUserUpdate(float fElapsedTime) override
|
|
|
|
{
|
|
|
|
Clear(olc::DARK_CYAN);
|
|
|
|
accumulatedTime+=fElapsedTime;
|
|
|
|
while (accumulatedTime>=UPDATE_RATE) {
|
|
|
|
accumulatedTime-=UPDATE_RATE;
|
|
|
|
updateGame(); //DO NOT ADD THINGS HERE. USE updateGame()!
|
|
|
|
}
|
|
|
|
if (GetKey(olc::F1).bPressed) {
|
|
|
|
ConsoleShow(olc::F1,false);
|
|
|
|
}
|
|
|
|
if (GetKey(olc::W).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[0]!=0)) {
|
|
|
|
meercaSpd[0]=0;
|
|
|
|
meercaSpd[1]=-moveSpd;
|
|
|
|
if (tailSize>0) {
|
|
|
|
tail[0].future_direction[tail[0].futureIndex]=UP;
|
|
|
|
tail[0].frameCount[tail[0].futureIndex]=TAIL_DISTANCE;
|
|
|
|
tail[0].futureIndex=(tail[0].futureIndex+1)%TAIL_DISTANCE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (GetKey(olc::A).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[1]!=0)) {
|
|
|
|
meercaSpd[0]=-moveSpd;
|
|
|
|
meercaSpd[1]=0;
|
|
|
|
if (tailSize>0) {
|
|
|
|
tail[0].future_direction[tail[0].futureIndex]=LEFT;
|
|
|
|
tail[0].frameCount[tail[0].futureIndex]=TAIL_DISTANCE;
|
|
|
|
tail[0].futureIndex=(tail[0].futureIndex+1)%TAIL_DISTANCE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (GetKey(olc::D).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[1]!=0)) {
|
|
|
|
meercaSpd[0]=moveSpd;
|
|
|
|
meercaSpd[1]=0;
|
|
|
|
if (tailSize>0) {
|
|
|
|
tail[0].future_direction[tail[0].futureIndex]=RIGHT;
|
|
|
|
tail[0].frameCount[tail[0].futureIndex]=TAIL_DISTANCE;
|
|
|
|
tail[0].futureIndex=(tail[0].futureIndex+1)%TAIL_DISTANCE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (GetKey(olc::S).bPressed&&(meercaSpd[0]+meercaSpd[1]==0||meercaSpd[0]!=0)) {
|
|
|
|
meercaSpd[0]=0;
|
|
|
|
meercaSpd[1]=moveSpd;
|
|
|
|
if (tailSize>0) {
|
|
|
|
tail[0].future_direction[tail[0].futureIndex]=DOWN;
|
|
|
|
tail[0].frameCount[tail[0].futureIndex]=TAIL_DISTANCE;
|
|
|
|
tail[0].futureIndex=(tail[0].futureIndex+1)%TAIL_DISTANCE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// called once per frame
|
|
|
|
if (meercaSpd[0]==moveSpd) {
|
|
|
|
DrawRotatedDecal({meercaPos[0],meercaPos[1]},meerca,M_PI_2,{16,16});
|
|
|
|
} else
|
|
|
|
if (meercaSpd[0]==-moveSpd) {
|
|
|
|
DrawRotatedDecal({meercaPos[0],meercaPos[1]},meerca,M_PI_2*3,{16,16});
|
|
|
|
} else
|
|
|
|
if (meercaSpd[1]==moveSpd) {
|
|
|
|
DrawRotatedDecal({meercaPos[0],meercaPos[1]},meerca,M_PI,{16,16});
|
|
|
|
} else {
|
|
|
|
DrawRotatedDecal({meercaPos[0],meercaPos[1]},meerca,0,{16,16});
|
|
|
|
}
|
|
|
|
DrawRotatedDecal({neggPos[0],neggPos[1]},negg,0,{16,16},{1.5,1.5},olc::YELLOW);
|
|
|
|
for (int i=0;i<badNeggCount;i++) {
|
|
|
|
DrawRotatedDecal({badNeggs[i*2],badNeggs[i*2+1]},badnegg,0,{16,16},{1,1});
|
|
|
|
}
|
|
|
|
for (int i=0;i<tailSize;i++) {
|
|
|
|
TailData t = tail[i];
|
|
|
|
DrawCircle({t.pos[0],t.pos[1]},8);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateGame() {
|
|
|
|
meercaPos[0]+=meercaSpd[0];
|
|
|
|
meercaPos[1]+=meercaSpd[1];
|
|
|
|
|
|
|
|
if (abs(meercaPos[0]-neggPos[0])<8&&abs(meercaPos[1]-neggPos[1])<8) {
|
|
|
|
assignNeggRandomPos();
|
|
|
|
tail=(TailData*)realloc(tail,sizeof(TailData)*++tailSize);
|
|
|
|
tail[tailSize-1].future_direction = (char*)malloc(sizeof(char)*TAIL_DISTANCE);
|
|
|
|
tail[tailSize-1].pos = (int*)malloc(sizeof(int)*2);
|
|
|
|
tail[tailSize-1].frameCount = (int*)malloc(sizeof(int)*TAIL_DISTANCE);
|
|
|
|
if (meercaSpd[0]==moveSpd) {
|
|
|
|
tail[tailSize-1].direction=RIGHT;
|
|
|
|
} else
|
|
|
|
if (meercaSpd[0]==-moveSpd) {
|
|
|
|
tail[tailSize-1].direction=LEFT;
|
|
|
|
} else
|
|
|
|
if (meercaSpd[1]==moveSpd) {
|
|
|
|
tail[tailSize-1].direction=DOWN;
|
|
|
|
} else
|
|
|
|
if (meercaSpd[1]==-moveSpd) {
|
|
|
|
tail[tailSize-1].direction=UP;
|
|
|
|
}
|
|
|
|
tail[tailSize-1].pos[0]=meercaPos[0];
|
|
|
|
tail[tailSize-1].pos[1]=meercaPos[1];
|
|
|
|
for (int i=0;i<TAIL_DISTANCE;i++) {
|
|
|
|
tail[tailSize-1].frameCount[i]=-1;
|
|
|
|
tail[tailSize-1].future_direction[i]=-1;
|
|
|
|
}
|
|
|
|
tail[tailSize-1].waitTime=TAIL_DISTANCE*tailSize;
|
|
|
|
tail[tailSize-1].futureIndex=0;
|
|
|
|
if ((float)rand()/(float)RAND_MAX<0.6) {
|
|
|
|
spawnBadNegg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i=0;i<badNeggCount;i++) {
|
|
|
|
if (abs(meercaPos[0]-badNeggs[i*2])<6&&abs(meercaPos[1]-badNeggs[i*2+1])<6) {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i=0;i<tailSize;i++) {
|
|
|
|
if (tail[i].waitTime>0) {
|
|
|
|
tail[i].waitTime--;
|
|
|
|
} else {
|
|
|
|
for (int j=0;j<TAIL_DISTANCE;j++) {
|
|
|
|
if (tail[i].frameCount[j]>=0) {
|
|
|
|
tail[i].frameCount[j]--;
|
|
|
|
if (tail[i].frameCount[j]==-1) {
|
|
|
|
tail[i].direction=tail[i].future_direction[j];
|
|
|
|
if (i+1<tailSize) {
|
|
|
|
tail[i+1].future_direction[tail[i+1].futureIndex]=tail[i].future_direction[j];
|
|
|
|
tail[i+1].frameCount[tail[i+1].futureIndex]=TAIL_DISTANCE;
|
|
|
|
tail[i+1].futureIndex=(tail[i+1].futureIndex+1)%TAIL_DISTANCE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (tail[i].direction) {
|
|
|
|
case UP:{
|
|
|
|
tail[i].pos[1]-=moveSpd;
|
|
|
|
}break;
|
|
|
|
case RIGHT:{
|
|
|
|
tail[i].pos[0]+=moveSpd;
|
|
|
|
}break;
|
|
|
|
case LEFT:{
|
|
|
|
tail[i].pos[0]-=moveSpd;
|
|
|
|
}break;
|
|
|
|
case DOWN:{
|
|
|
|
tail[i].pos[1]+=moveSpd;
|
|
|
|
}break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
MeercaChase game;
|
|
|
|
if (game.Construct(480, 480, 4, 4))
|
|
|
|
game.Start();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|