|
|
|
@ -5,62 +5,129 @@ using namespace olc; |
|
|
|
|
#define WIDTH 640 |
|
|
|
|
#define HEIGHT 480 |
|
|
|
|
|
|
|
|
|
class Example : public olc::PixelGameEngine |
|
|
|
|
{ |
|
|
|
|
struct Ball{ |
|
|
|
|
vf2d pos={}; |
|
|
|
|
vf2d vel={}; |
|
|
|
|
vf2d acc={}; |
|
|
|
|
float radius=0; |
|
|
|
|
float mass=0; |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class MiniGolfPro : public olc::PixelGameEngine |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
Example() |
|
|
|
|
std::vector<Ball>balls; |
|
|
|
|
Ball*selectedBall; |
|
|
|
|
|
|
|
|
|
MiniGolfPro() |
|
|
|
|
{ |
|
|
|
|
sAppName = "Example"; |
|
|
|
|
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: |
|
|
|
|
olc::Key last; |
|
|
|
|
bool OnUserCreate() override |
|
|
|
|
{ |
|
|
|
|
std::cout<<"Test"<<std::endl; |
|
|
|
|
ConsoleShow(olc::Key::A); |
|
|
|
|
for (int i=0;i<20;i++){ |
|
|
|
|
AddBall({float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},rand()%16+2); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool OnUserUpdate(float fElapsedTime) override |
|
|
|
|
{ |
|
|
|
|
std::cout<<"Test 4"<<std::endl; |
|
|
|
|
ConsoleCaptureStdOut(true); |
|
|
|
|
std::cout<<"Test 2"<<std::endl; |
|
|
|
|
ConsoleCaptureStdOut(false); |
|
|
|
|
std::cout<<"Test 3"<<std::endl; |
|
|
|
|
std::cout<<"A"<<std::endl; |
|
|
|
|
Clear(BLACK); |
|
|
|
|
DrawString({0,0},std::to_string(last)); |
|
|
|
|
return true; |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool OnUserDestroy()override{ |
|
|
|
|
return true; |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void GetAnyKeyPress(olc::Key pressed)override{ |
|
|
|
|
last=pressed; |
|
|
|
|
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(); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
enum Direction{ |
|
|
|
|
RIGHT, |
|
|
|
|
DOWN, |
|
|
|
|
LEFT, |
|
|
|
|
UP |
|
|
|
|
}; |
|
|
|
|
if(fabs(b.vel.x*b.vel.x+b.vel.y*b.vel.y)<0.01){ |
|
|
|
|
b.vel.x=0; |
|
|
|
|
b.vel.y=0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
struct Data{ |
|
|
|
|
int x,y; |
|
|
|
|
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() |
|
|
|
|
{
|
|
|
|
|
Example demo; |
|
|
|
|
MiniGolfPro demo; |
|
|
|
|
if (demo.Construct(640, 480, 4, 4)) |
|
|
|
|
demo.Start(); |
|
|
|
|
|
|
|
|
|