generated from sigonasr2/CPlusPlusProjectTemplate
Implement basic ball setup and selection
This commit is contained in:
parent
ecac78fd64
commit
3ad7d5a725
@ -1,7 +1,7 @@
|
|||||||
build.sh:c073187e65d0e23aa77aa94af4ec6382 -
|
build.sh:c073187e65d0e23aa77aa94af4ec6382 -
|
||||||
commit.sh:1af81bf417dfb932284d8a14fdd10657 -
|
commit.sh:1af81bf417dfb932284d8a14fdd10657 -
|
||||||
debug.sh:849488515cab075948653c15eec4177b -
|
debug.sh:8125f303032b6cbc137223df63d10096 -
|
||||||
lines.sh:3b907786f7fc9204025993016c9080de -
|
lines.sh:3b907786f7fc9204025993016c9080de -
|
||||||
release.sh:0ab321c3fa2f1a1b2f03b1aec3bce816 -
|
release.sh:b1ce8461a303e8e7aa9ed74259db3873 -
|
||||||
temp:d41d8cd98f00b204e9800998ecf8427e -
|
temp:d41d8cd98f00b204e9800998ecf8427e -
|
||||||
web.sh:4bbe9c5710a0ae4289468c3f7f340ff1 -
|
web.sh:dd7eec9825587db99063877da2839506 -
|
||||||
|
File diff suppressed because one or more lines are too long
Binary file not shown.
127
main.cpp
127
main.cpp
@ -5,62 +5,129 @@ using namespace olc;
|
|||||||
#define WIDTH 640
|
#define WIDTH 640
|
||||||
#define HEIGHT 480
|
#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:
|
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:
|
public:
|
||||||
olc::Key last;
|
|
||||||
bool OnUserCreate() override
|
bool OnUserCreate() override
|
||||||
{
|
{
|
||||||
std::cout<<"Test"<<std::endl;
|
for (int i=0;i<20;i++){
|
||||||
ConsoleShow(olc::Key::A);
|
AddBall({float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},rand()%16+2);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OnUserUpdate(float fElapsedTime) override
|
bool OnUserUpdate(float fElapsedTime) override
|
||||||
{
|
{
|
||||||
std::cout<<"Test 4"<<std::endl;
|
auto DoCirclesOverlap = [](vf2d pos1, float r1, vf2d pos2, float r2){
|
||||||
ConsoleCaptureStdOut(true);
|
return fabs((pos1.x - pos2.x)*(pos1.x - pos2.x) + (pos1.y - pos2.y)*(pos1.y - pos2.y)) <= (r1 + r2)*(r1 + r2);
|
||||||
std::cout<<"Test 2"<<std::endl;
|
};
|
||||||
ConsoleCaptureStdOut(false);
|
|
||||||
std::cout<<"Test 3"<<std::endl;
|
auto IsPointInCircle = [](vf2d pos, float r, vf2d point){
|
||||||
std::cout<<"A"<<std::endl;
|
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);
|
Clear(BLACK);
|
||||||
DrawString({0,0},std::to_string(last));
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OnUserDestroy()override{
|
bool OnUserDestroy()override{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetAnyKeyPress(olc::Key pressed)override{
|
|
||||||
last=pressed;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
enum Direction{
|
|
||||||
RIGHT,
|
|
||||||
DOWN,
|
|
||||||
LEFT,
|
|
||||||
UP
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Data{
|
|
||||||
int x,y;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Example demo;
|
MiniGolfPro demo;
|
||||||
if (demo.Construct(640, 480, 4, 4))
|
if (demo.Construct(640, 480, 4, 4))
|
||||||
demo.Start();
|
demo.Start();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user