generated from sigonasr2/CPlusPlusProjectTemplate
Really cool!
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
95e9c58458
commit
dd278bd15a
Binary file not shown.
214
main.cpp
214
main.cpp
@ -4,86 +4,150 @@
|
|||||||
|
|
||||||
using namespace olc;
|
using namespace olc;
|
||||||
|
|
||||||
class Example : public olc::PixelGameEngine
|
void tailFollow(vi2d&tail,std::array<vi2d,9>&head,std::map<std::string,bool>&posmap){
|
||||||
{
|
//std::cout<<head<<"//"<<tail<<std::endl;
|
||||||
public:
|
for (int i=1;i<9;i++){
|
||||||
Example()
|
if (std::abs(head[i-1].x-head[i].x)>=2&&std::abs(head[i-1].y-head[i].y)>=1||
|
||||||
{
|
std::abs(head[i-1].x-head[i].x)>=1&&std::abs(head[i-1].y-head[i].y)>=2){
|
||||||
sAppName = "Example";
|
head[i].x+=(head[i-1].x-head[i].x)/std::abs(head[i-1].x-head[i].x);
|
||||||
}
|
head[i].y+=(head[i-1].y-head[i].y)/std::abs(head[i-1].y-head[i].y);
|
||||||
|
} else
|
||||||
public:
|
if (std::abs(head[i-1].x-head[i].x)>=2){
|
||||||
bool RayVsRect(const vf2d ray_origin, const vf2d ray_dir, const olc::utils::geom2d::rect<float> target, vf2d&contact_point, vf2d&contact_normal, float&t_hit_near){
|
//std::cout<<" "<<"Move X: "<<std::endl;
|
||||||
|
//std::cout<<" "<<head<<"//"<<head[i]<<std::endl;
|
||||||
contact_normal = { 0, 0 };
|
head[i].x+=(head[i-1].x-head[i].x)/std::abs(head[i-1].x-head[i].x);
|
||||||
contact_point = { 0, 0 };
|
} else
|
||||||
|
if (std::abs(head[i-1].y-head[i].y)>=2){
|
||||||
vf2d t_near = {(target.pos.x - ray_origin.x) / ray_dir.x, (target.pos.y - ray_origin.y) / ray_dir.y};
|
//std::cout<<" "<<"Move Y: "<<std::endl;
|
||||||
vf2d t_far = {(target.pos.x + target.size.x - ray_origin.x) / ray_dir.x, (target.pos.y + target.size.y - ray_origin.y) / ray_dir.y};
|
//std::cout<<" "<<head<<"//"<<head[i]<<std::endl;
|
||||||
|
head[i].y+=(head[i-1].y-head[i].y)/std::abs(head[i-1].y-head[i].y);
|
||||||
if (t_near.x > t_far.x) {float b; b = t_near.x; t_near.x = t_far.x; t_far.x = b;};
|
|
||||||
if (t_near.y > t_far.y) {float b; b = t_near.y; t_near.y = t_far.y; t_far.y = b;};
|
|
||||||
|
|
||||||
if (t_near.x > t_far.y || t_near.y > t_far.x) return false;
|
|
||||||
|
|
||||||
t_hit_near = fmax(t_near.x, t_near.y);
|
|
||||||
float t_hit_far = fmin(t_far.x, t_far.y);
|
|
||||||
|
|
||||||
if (t_hit_far < 0) return false;
|
|
||||||
|
|
||||||
contact_point.x = ray_origin.x + t_hit_near * ray_dir.x;
|
|
||||||
contact_point.y = ray_origin.y + t_hit_near * ray_dir.y;
|
|
||||||
|
|
||||||
if (t_near.x > t_near.y)
|
|
||||||
if ( 1.0f / ray_dir.x < 0)
|
|
||||||
contact_normal = { 1, 0 };
|
|
||||||
else
|
|
||||||
contact_normal = { -1, 0};
|
|
||||||
else
|
|
||||||
if ( t_near.x < t_near.y)
|
|
||||||
if ( 1.0f / ray_dir.y < 0)
|
|
||||||
contact_normal = { 0, 1 };
|
|
||||||
else
|
|
||||||
contact_normal = { 0, -1 };
|
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
vf2d originPoint={16,16};
|
|
||||||
bool OnUserCreate() override
|
|
||||||
{
|
|
||||||
// Called once at the start, so create things here
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OnUserUpdate(float fElapsedTime) override
|
|
||||||
{
|
|
||||||
vf2d velocity={(GetKey(D).bHeld-GetKey(A).bHeld)*20*fElapsedTime,(GetKey(S).bHeld-GetKey(W).bHeld)*20*fElapsedTime};
|
|
||||||
vf2d contact_point;
|
|
||||||
vf2d contact_normal;
|
|
||||||
float t_hit_near;
|
|
||||||
|
|
||||||
Clear(Pixel(64,64,255));
|
|
||||||
if (!olc::utils::geom2d::overlaps(olc::utils::geom2d::circle<float>{originPoint+velocity,5},olc::utils::geom2d::rect<float>{{32,32},{64,32}})) {
|
|
||||||
originPoint+=velocity;
|
|
||||||
DrawCircle(originPoint,5);
|
|
||||||
} else {
|
|
||||||
DrawCircle(originPoint,5,RED);
|
|
||||||
}
|
}
|
||||||
DrawLine(originPoint,GetMousePos());
|
}
|
||||||
|
if (std::abs(head[8].x-tail.x)>=2&&std::abs(head[8].y-tail.y)>=1||
|
||||||
DrawRect({32,32},{64,32},RayVsRect(originPoint, GetMousePos()-originPoint, olc::utils::geom2d::rect<float>{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE);
|
std::abs(head[8].x-tail.x)>=1&&std::abs(head[8].y-tail.y)>=2){
|
||||||
return true;
|
tail.x+=(head[8].x-tail.x)/std::abs(head[8].x-tail.x);
|
||||||
}
|
tail.y+=(head[8].y-tail.y)/std::abs(head[8].y-tail.y);
|
||||||
};
|
} else
|
||||||
|
if (std::abs(head[8].x-tail.x)>=2){
|
||||||
|
//std::cout<<" "<<"Move X: "<<std::endl;
|
||||||
|
//std::cout<<" "<<head<<"//"<<tail<<std::endl;
|
||||||
|
tail.x+=(head[8].x-tail.x)/std::abs(head[8].x-tail.x);
|
||||||
|
} else
|
||||||
|
if (std::abs(head[8].y-tail.y)>=2){
|
||||||
|
//std::cout<<" "<<"Move Y: "<<std::endl;
|
||||||
|
//std::cout<<" "<<head<<"//"<<tail<<std::endl;
|
||||||
|
tail.y+=(head[8].y-tail.y)/std::abs(head[8].y-tail.y);
|
||||||
|
}
|
||||||
|
//std::cout<<"Added "<<std::to_string(tail.x)+"_"+std::to_string(tail.y)<<std::endl;
|
||||||
|
posmap[std::to_string(tail.x)+"_"+std::to_string(tail.y)]=true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DrawMap(vi2d&tail,std::array<vi2d,9>&head,std::map<std::string,bool>&posmap){
|
||||||
|
int maxX=tail.x;
|
||||||
|
int maxY=tail.y;
|
||||||
|
int minX=tail.x;
|
||||||
|
int minY=tail.y;
|
||||||
|
for (int i=0;i<9;i++){
|
||||||
|
maxX=std::max(maxX,head[i].x);
|
||||||
|
maxY=std::max(maxY,head[i].y);
|
||||||
|
minX=std::min(minX,head[i].x);
|
||||||
|
minY=std::min(minY,head[i].y);
|
||||||
|
}
|
||||||
|
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
|
||||||
|
if (std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str())>maxX){
|
||||||
|
maxX=std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
|
||||||
|
if (std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str())>maxY){
|
||||||
|
maxY=std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
|
||||||
|
if (std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str())<minX){
|
||||||
|
minX=std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
|
||||||
|
if (std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str())<minY){
|
||||||
|
minY=std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int y=minY;y<=maxX;y++){
|
||||||
|
for (int x=minX;x<=maxX;x++){
|
||||||
|
if (tail.x==x&&tail.y==y){
|
||||||
|
std::cout<<"T";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (int i=0;i<9;i++){
|
||||||
|
if (head[i].x==x&&head[i].y==y){
|
||||||
|
std::cout<<i;
|
||||||
|
goto next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (std::map<std::string,bool>::iterator it=posmap.begin();it!=posmap.end();it++){
|
||||||
|
if (std::atoi(it->first.substr(0,it->first.find_first_of('_')).c_str())==x&&
|
||||||
|
std::atoi(it->first.substr(it->first.find_first_of('_')+1,std::string::npos).c_str())==y){
|
||||||
|
std::cout<<"#";
|
||||||
|
goto next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<".";
|
||||||
|
next:;
|
||||||
|
}
|
||||||
|
std::cout<<std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Example demo;
|
std::ifstream file("input");
|
||||||
if (demo.Construct(128, 120, 8, 8))
|
std::map<std::string,bool>posmap;
|
||||||
demo.Start();
|
vi2d tail={0,0};
|
||||||
|
std::array<vi2d,9>head={vi2d{0,0}};
|
||||||
|
while (file.good()){
|
||||||
|
std::string line;
|
||||||
|
std::getline(file,line);
|
||||||
|
posmap[std::to_string(tail.x)+"_"+std::to_string(tail.y)]=true;
|
||||||
|
if (line.length()>0){
|
||||||
|
char dir=line[0];
|
||||||
|
int amt=std::atoi(line.substr(1,std::string::npos).c_str());
|
||||||
|
std::cout<<dir<<" "<<amt<<std::endl;
|
||||||
|
switch (dir){
|
||||||
|
case 'D':{
|
||||||
|
while (amt>0){
|
||||||
|
head[0].y++;
|
||||||
|
tailFollow(tail,head,posmap);
|
||||||
|
amt--;
|
||||||
|
}
|
||||||
|
}break;
|
||||||
|
case 'R':{
|
||||||
|
while (amt>0){
|
||||||
|
head[0].x++;
|
||||||
|
tailFollow(tail,head,posmap);
|
||||||
|
amt--;
|
||||||
|
}
|
||||||
|
}break;
|
||||||
|
case 'L':{
|
||||||
|
while (amt>0){
|
||||||
|
head[0].x--;
|
||||||
|
tailFollow(tail,head,posmap);
|
||||||
|
amt--;
|
||||||
|
}
|
||||||
|
}break;
|
||||||
|
case 'U':{
|
||||||
|
while (amt>0){
|
||||||
|
head[0].y--;
|
||||||
|
tailFollow(tail,head,posmap);
|
||||||
|
amt--;
|
||||||
|
}
|
||||||
|
}break;
|
||||||
|
}
|
||||||
|
//DrawMap(tail,head,posmap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DrawMap(tail,head,posmap);
|
||||||
|
std::cout<<"Key count:"<<posmap.size()<<std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user