diff --git a/Star_Forge_0.1/celestial_body.cpp b/Star_Forge_0.1/celestial_body.cpp index 302d08e..2fb628d 100644 --- a/Star_Forge_0.1/celestial_body.cpp +++ b/Star_Forge_0.1/celestial_body.cpp @@ -167,3 +167,124 @@ Celestial_Body Celestial_Body::operator -(Phase_vector a) res.v_y-=a.v_y; return res; } + +Celestial_Body Celestial_Body::operator =(Celestial_Body a) +{ + x = a.x; + y = a.y; + v_x = a.v_x; + v_y = a.v_y; + Radius = a.Radius; + Mass = a.Mass; +} + +/*int Celestial_Body::operator ==(Celestial_Body a) +{ + if ((x == a.x)&&(y == a.y)&&(v_x == a.v_x)&&(v_y == a.v_y)&&(w_y == a.w_y)&&(w_y == a.w_y)&&(Radius == a.Radius)&&(Mass == a.Mass)) + return true; + else return false; +} +int Celestial_Body::operator !=(Celestial_Body a) +{ + if ((x != a.x)||(y != a.y)||(v_x != a.v_x)||(v_y != a.v_y)||(w_y != a.w_y)||(w_y != a.w_y)||(Radius != a.Radius)||(Mass != a.Mass)) + return true; + else return false; +}*/ + +//Atlas section + +Atlas::Atlas() +{ + first = NULL; + last = NULL; + amount = 0; +} + +Atlas::Atlas(Celestial_Body a) +{ + Atlas_node tmp = new Atlas_node_el; + tmp->body = a; + tmp->next = NULL; + CircleShape avat(a.Radius); + tmp->avatar = avat; + first = tmp; + last = tmp; + amount = 1; +} + +void Atlas::add(Celestial_Body a) +{ + Atlas_node tmp = new Atlas_node_el; + tmp->body = a; + tmp->next = NULL; + CircleShape avat(a.Radius); + tmp->avatar = avat; + cout<<"1\n"; + if(last != NULL) + { + last->next = tmp; + cout<<"2\n"; + last = last -> next; + cout<<"3\n"; + } + else + { + last = tmp; + first = tmp; + } + amount++; + return; +} + +/*void Atlas::remove(Celestial_Body* a) +{ + Celestial_Body* tmp = first, tmp2; + if (first == a) + { + first = first->next; + free(tmp); + return; + } + while (tmp != NULL) + { + if(tmp->next == a) + { + tmp2 = tmp->next; + tmp->next = tmp->next->next; + free(tmp2); + } + } +}*/ + +void Atlas::del() +{ + Atlas_node tmp1 = first; + while (tmp1 != NULL) + { + Atlas_node tmp2 = tmp1; + tmp1 = tmp1->next; + delete(tmp2); + } +} + + + + + + + + + + + + + + + + + + + + + + diff --git a/Star_Forge_0.1/celestial_body.h b/Star_Forge_0.1/celestial_body.h index b3885b2..5f351a4 100644 --- a/Star_Forge_0.1/celestial_body.h +++ b/Star_Forge_0.1/celestial_body.h @@ -2,6 +2,8 @@ #define CELESTIAL_BODY #include +#include +using namespace sf; class Vector { @@ -61,6 +63,26 @@ class Phase_vector: public Vector Phase_vector operator /(float a); Phase_vector operator +(Phase_vector a); Phase_vector operator -(Phase_vector a); + /////////////////////////////////////Секция ввода/вывода + friend std::ostream& operator<<(std::ostream& os, Phase_vector& a) + { + os<<"("<>(std::istream& os, Phase_vector& a) + { + float c, b, f, g; + os>>c; + os>>b; + os>>f; + os>>g; + a.x = c; + a.y = b; + a.v_x=f; + a.v_y=g; + return os; + } }; /////////////////////////////////////////////Celestial Body class is an expansion of Phase_vector class @@ -71,14 +93,47 @@ class Celestial_Body: public Phase_vector float w_y; float Radius; float Mass; + Color color; + Texture texture; Celestial_Body(); Celestial_Body(const Celestial_Body& other); Celestial_Body(float a, float b, float c, float d, float e, float f, float g, float k); + Celestial_Body operator =(Celestial_Body a); Celestial_Body operator +(Phase_vector a); Celestial_Body operator -(Phase_vector a); + int operator ==(Celestial_Body a); + int operator !=(Celestial_Body a); + friend std::ostream& operator<<(std::ostream& os, Celestial_Body& a) + { + os<<"("< +#include +#include "method.cpp" +#include "celestial_body.cpp" +using namespace sf; +int main() +{ + Clock clock; + RenderWindow window(VideoMode(WIDTH,WIDTH),"test"); + Atlas atl; + Celestial_Body Sun(0,0,0,0,0,0,5,1.9885e30), Earth(152.1e9,0,0,29.783e3,0,0,1,5.9726e24), Mars(206.62e9 , 0, 0, 26.50e3, 0, 0, 1, 0.64171e24); + Celestial_Body Mercury(46e9, 0, 0, 58.98e3, 0, 0, 1, 0.33011e24), Venus(107.48e9, 0, 0, 35.26e3, 0, 0, 1, 4.8675e24);//, Jupiter(740.52e9, 0, 0, 13.72e3, 0, 0, 3, 1898.19e24); + /*Atlas_node Sunn = new Atlas_node_el; + Sunn->body = Sun; + CircleShape sunav(Sun.Radius); + Sunn->avatar = sunav; + Atlas_node Earthn = new Atlas_node_el; + Earthn->body = Earth; + CircleShape earthav(Earth.Radius); + Earthn->avatar = earthav; + Sunn->next = Earthn; + Earthn->next = NULL; + atl.first = Sunn; + atl.last = Earthn; + atl.amount = 2;*/ + atl.add(Sun); + atl.add(Earth); + atl.add(Mars); + atl.add(Mercury); + atl.add(Venus); + //atl.add(Jupiter); + //Planet2.setPosition(Mars.x,Mars.y); + Phase_vector v1,v2; + while (window.isOpen()) + { + float time = clock.getElapsedTime().asMicroseconds(); //дать прошедшее время в микросекундах + clock.restart(); //перезагружает время + time = T_SCALE * time; + Motion(atl, time, WIDTH); + Event event; + while (window.pollEvent(event)) + { + if(event.type == Event::KeyPressed && event.key.code == Keyboard::Escape) + window.close(); + } + window.clear(); + draw(atl, &window); + window.display(); + } + atl.del(); + return 0; +} diff --git a/Star_Forge_0.1/method.cpp b/Star_Forge_0.1/method.cpp index 955a18f..e084292 100644 --- a/Star_Forge_0.1/method.cpp +++ b/Star_Forge_0.1/method.cpp @@ -1,43 +1,85 @@ -#define G 1 +#define G 6.67408e-11 #include "celestial_body.h" #include "math.h" + +float x(float x_m, float scale) +{ + float res = (x_m * (scale))/(6.084e11) + scale/2; + //std::cout<<"x: "<< + return res; +} + +float y(float y_m, float scale) +{ + float res = scale/2 - (y_m * scale)/(6.084e11); + return res; +} + float distance(Celestial_Body a, Celestial_Body b) { float dist = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); return dist; } -Phase_vector f(Celestial_Body a, Celestial_Body b) +Phase_vector f(Celestial_Body a, Atlas atl) //Тут координатам соответствуют скорости, а скоростям ускорения { - float dist = distance(a, b); + float dist; + Atlas_node tmp = atl.first; Phase_vector res = Phase_vector(); res.x = a.v_x; - res.x = a.v_y; - res.v_x =(G * a.Mass * b.Mass * (b.x - a.x) )/ (dist * dist * dist); - res.v_y = (G * a.Mass * b.Mass * (b.y - a.y)) / (dist * dist * dist); + res.y = a.v_y; + res.v_x = 0; + res.v_y = 0; + while (tmp != NULL) + { + if(a.Mass != (tmp -> body.Mass)) + { + dist = distance(a, tmp->body); + res.v_x = res.v_x + (G * tmp->body.Mass * (tmp->body.x - a.x) )/(dist * dist * dist); + res.v_y = res.v_y + (G * tmp->body.Mass * (tmp->body.y - a.y))/(dist * dist * dist); + std::cout<<"v_x"<next; + } return res; } -Phase_vector k1(Celestial_Body a, Celestial_Body b, float t_scale) +Phase_vector k1(Celestial_Body& a, Atlas& atl, float t_scale) { - return f(a, b); + return f(a, atl); } -Phase_vector k2(Celestial_Body a, Celestial_Body b, float t_scale, Phase_vector k1) +Phase_vector k2(Celestial_Body& a, Atlas& atl, float t_scale, Phase_vector& k1) { - return f((a + (k1 * t_scale) / 2), b);//Второе тело неподвижно? + return f((a + (k1 * t_scale) / 2), atl);//Второе тело неподвижно? } -Phase_vector k3(Celestial_Body a, Celestial_Body b, float t_scale, Phase_vector k2) +Phase_vector k3(Celestial_Body& a, Atlas& atl, float t_scale, Phase_vector& k2) { - return f((a + (k2 * t_scale) / 2), b);//Второе тело неподвижно? + return f((a + (k2 * t_scale) / 2), atl);//Второе тело неподвижно? } -Phase_vector k4(Celestial_Body a, Celestial_Body b, float t_scale, Phase_vector k3) +Phase_vector k4(Celestial_Body& a, Atlas& atl, float t_scale, Phase_vector& k3) { - return f((a + (k3 * t_scale)), b);//Второе тело неподвижно? + return f((a + (k3 * t_scale)), atl);//Второе тело неподвижно? +} + +Phase_vector Movement(Celestial_Body& a, Atlas& atl, float t_scale) +{ + Phase_vector res = Phase_vector(); + Phase_vector k12 = k1(a, atl, t_scale); + std::cout<<"k1"<body, atl, t_scale); + tmp->body = tmp->body+ mov; + tmp->avatar.setPosition(x(tmp->body.x, scale), y(tmp->body.y, scale)); + tmp = tmp->next; + } + return atl; +} + +void draw(Atlas atl, RenderWindow* window) +{ + Atlas_node tmp = atl.first; + while(tmp != NULL) + { + window->draw(tmp->avatar); + tmp = tmp->next; + } + return; +} diff --git a/blast.jpg b/blast.jpg deleted file mode 100644 index 227854b..0000000 Binary files a/blast.jpg and /dev/null differ diff --git a/sfml_test.cpp b/sfml_test.cpp deleted file mode 100644 index 634d57a..0000000 --- a/sfml_test.cpp +++ /dev/null @@ -1,144 +0,0 @@ -#define RAND_MAX 900 -#include -#include -#include -#include -#include -//#include -using namespace sf; -using namespace std; - -int main() -{ - Vector2 mouse_Position; - Vector2 bullet_Position; - Vector2 shape_Position; - Vector2 target_Position; - Vector2 ufo_bullet_Position; - int m = 1, fire = 0, win = 0, time = 0, death = 0, phase = 0; - RenderWindow window(sf::VideoMode(1000, 600), "SFML works!"); - sf::Font font; - Texture ship; - ship.loadFromFile("spaceship2.jpg", IntRect(0, 0, 215, 215)); - Texture blast; - blast.loadFromFile("blast.jpg", IntRect(0, 0, 151, 151)); - Texture ufo; - ufo.loadFromFile("ufo.jpg", IntRect(0, 0, 400, 400)); - font.loadFromFile("arial.ttf"); - Text text("Score: %d", font, win); - text.setCharacterSize(30); - text.setStyle(sf::Text::Bold); - text.setColor(sf::Color::Red); - text.setPosition(500, 30); - RectangleShape shape(Vector2f(30,30)); - shape.rotate(90); - CircleShape bullet(10.f); - CircleShape ufo_bullet(10.f); - RectangleShape target(Vector2f(40,40)); - shape.setPosition(30, 450); - bullet.setPosition(0,0); - ufo_bullet.setPosition(0,0); - target.setPosition(900, 300); - shape.setTexture(&ship); - bullet.setTexture(&blast); - ufo_bullet.setTexture(&blast); - target.setTexture(&ufo); - srand(5); - window.setMouseCursorVisible(0); - - Text game_over("Game over. Press R to retry.", font); - game_over.setCharacterSize(30); - game_over.setStyle(sf::Text::Bold); - game_over.setColor(sf::Color::Red); - game_over.setPosition(500, 300); - - std::ostringstream playerScoreString; // объявили переменную - playerScoreString << win; //занесли в нее число очков, то есть формируем строку - text.setString("Score:" + playerScoreString.str());//задаем строку тексту и вызываем сформированную выше строку методом .str() - text.setPosition(500, 100);//задаем позицию текста, отступая от центра камеры - - while (window.isOpen()) - { - mouse_Position = sf::Mouse::getPosition(); - ufo_bullet_Position = ufo_bullet.getPosition(); - shape.setPosition(mouse_Position.x, mouse_Position.y); - target_Position = target.getPosition(); - bullet_Position = bullet.getPosition(); - if (bullet_Position.x >= 1000) fire = 0; - if (ufo_bullet_Position.x <= 0) - { - ufo_bullet.setPosition(target_Position.x, target_Position.y); - phase = rand(); - } - //std::cout<= (mouse_Position.y - 10))&&(abs(ufo_bullet_Position.x - mouse_Position.x) < 10)&&(ufo_bullet_Position.y <= (mouse_Position.y + 10))) - { - death = 1; - } - if((bullet_Position.y >= (target_Position.y - 10))&&(abs(bullet_Position.x - target_Position.x) < 10)&&(bullet_Position.y <= (target_Position.y + 30))) - { - win++; - target.setPosition(target_Position.x,50 + rand()%500); - } - //if (m >= 1000) - //{ - // shape.setPosition(0, 0); - // m = 0; - // fire = 0; - // bullet.setPosition(0, 0); - //} - //shape.move(0, 1); - ufo_bullet.move(-3, sin((ufo_bullet_Position.x + phase)/100)); - target.move(0, sin(time/100)); - if(fire == 1) bullet.move(5, 0); - while (window.pollEvent(event)) - { - if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) - { - if(fire == 0) - { - fire = 1; - bullet_Position = shape.getPosition(); - bullet.setPosition(bullet_Position.x, bullet_Position.y); - } - } - if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Space)) - { - if(fire == 0) - { - fire = 1; - bullet_Position = shape.getPosition(); - bullet.setPosition(bullet_Position.x, bullet_Position.y); - } - } - if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::R) && (death == 1)) - { - death = 0; - } - if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::W)) shape.move(0, -10); - if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::S)) shape.move(0, 10); - if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::D)) shape.move(10, 0); - if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::A)) shape.move(-10, 0); - if (event.type == Event::Closed) - window.close(); - } - time ++; - - window.clear(); - - if(death == 0) - { - if(fire == 1) window.draw(bullet); - window.draw(text); - window.draw(shape); - window.draw(target); - window.draw(ufo_bullet); - } - else window.draw(game_over); - window.display(); - } - - return 0; -} diff --git a/sfml_test.exe b/sfml_test.exe deleted file mode 100755 index 86e1c48..0000000 Binary files a/sfml_test.exe and /dev/null differ diff --git a/sfml_test.o b/sfml_test.o deleted file mode 100644 index 0c315e3..0000000 Binary files a/sfml_test.o and /dev/null differ diff --git a/ship.tif b/ship.tif deleted file mode 100644 index 211ba49..0000000 Binary files a/ship.tif and /dev/null differ diff --git a/spaceship.png b/spaceship.png deleted file mode 100644 index 90fcacc..0000000 Binary files a/spaceship.png and /dev/null differ diff --git a/spaceship2.jpg b/spaceship2.jpg deleted file mode 100644 index 6ec6e20..0000000 Binary files a/spaceship2.jpg and /dev/null differ diff --git a/ufo.jpg b/ufo.jpg deleted file mode 100644 index 14f4c01..0000000 Binary files a/ufo.jpg and /dev/null differ