|
|
|
#include "pixelGameEngine.h"
|
|
|
|
|
|
|
|
using namespace olc;
|
|
|
|
|
|
|
|
#define WIDTH 640
|
|
|
|
#define HEIGHT 480
|
|
|
|
|
|
|
|
struct Ball{
|
|
|
|
vf2d pos={};
|
|
|
|
vf2d vel={};
|
|
|
|
vf2d acc={};
|
|
|
|
float radius=0;
|
|
|
|
float mass=0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class MiniGolfPro : public olc::PixelGameEngine
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::vector<Ball>balls;
|
|
|
|
Ball*selectedBall;
|
|
|
|
|
|
|
|
MiniGolfPro()
|
|
|
|
{
|
|
|
|
sAppName = "Mini Golf Pro";
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddBall(vf2d pos,float r){
|
|
|
|
Ball b;
|
|
|
|
b.pos=pos;
|
|
|
|
b.radius=r;
|
|
|
|
b.mass=r*10;
|
|
|
|
balls.emplace_back(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool OnUserCreate() override
|
|
|
|
{
|
|
|
|
for (int i=0;i<20;i++){
|
|
|
|
AddBall({float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},rand()%16+2);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnUserUpdate(float fElapsedTime) override
|
|
|
|
{
|
|
|
|
auto DoCirclesOverlap = [](vf2d pos1, float r1, vf2d pos2, float r2){
|
|
|
|
return fabs((pos1.x - pos2.x)*(pos1.x - pos2.x) + (pos1.y - pos2.y)*(pos1.y - pos2.y)) <= (r1 + r2)*(r1 + r2);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto IsPointInCircle = [](vf2d pos, float r, vf2d point){
|
|
|
|
return fabs((pos.x - point.x)*(pos.x - point.x) + (pos.y - point.y)*(pos.y - point.y)) < (r * r);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (GetMouse(0).bPressed||GetMouse(1).bPressed){
|
|
|
|
selectedBall = nullptr;
|
|
|
|
for (Ball&b:balls){
|
|
|
|
if (IsPointInCircle(b.pos,b.radius,GetMousePos()))
|
|
|
|
{
|
|
|
|
selectedBall = &b;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(GetMouse(0).bHeld){
|
|
|
|
if(selectedBall!=nullptr){
|
|
|
|
selectedBall->pos.x=GetMouseX();
|
|
|
|
selectedBall->pos.y=GetMouseY();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(GetMouse(0).bReleased){
|
|
|
|
selectedBall=nullptr;
|
|
|
|
}
|
|
|
|
if(GetMouse(1).bReleased){
|
|
|
|
if(selectedBall!=nullptr){
|
|
|
|
selectedBall->vel.x=5*(selectedBall->pos.x-(float)GetMouseX());
|
|
|
|
selectedBall->vel.y=5*(selectedBall->pos.y-(float)GetMouseY());
|
|
|
|
}
|
|
|
|
selectedBall=nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Clear(BLACK);
|
|
|
|
for(Ball&b:balls){
|
|
|
|
b.acc.x=-b.vel.x*0.8f;
|
|
|
|
b.acc.y=-b.vel.y*0.8f;
|
|
|
|
b.vel.x+=b.acc.x*fElapsedTime;
|
|
|
|
b.vel.y+=b.acc.y*fElapsedTime;
|
|
|
|
b.pos.x+=b.vel.x*fElapsedTime;
|
|
|
|
b.pos.y+=b.vel.y*fElapsedTime;
|
|
|
|
|
|
|
|
if(b.pos.x<0){
|
|
|
|
b.pos.x+=ScreenWidth();
|
|
|
|
}
|
|
|
|
if(b.pos.y<0){
|
|
|
|
b.pos.y+=ScreenHeight();
|
|
|
|
}
|
|
|
|
if(b.pos.x>=ScreenWidth()){
|
|
|
|
b.pos.x-=ScreenWidth();
|
|
|
|
}
|
|
|
|
if(b.pos.y>=ScreenHeight()){
|
|
|
|
b.pos.y-=ScreenHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fabs(b.vel.x*b.vel.x+b.vel.y*b.vel.y)<0.01){
|
|
|
|
b.vel.x=0;
|
|
|
|
b.vel.y=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(Ball&b:balls){
|
|
|
|
if(selectedBall==&b){
|
|
|
|
FillCircle(b.pos,b.radius,YELLOW);
|
|
|
|
} else {
|
|
|
|
FillCircle(b.pos,b.radius);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnUserDestroy()override{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
MiniGolfPro demo;
|
|
|
|
if (demo.Construct(640, 480, 4, 4))
|
|
|
|
demo.Start();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|