diff --git a/animal.cpp b/animal.cpp new file mode 100644 index 0000000..2679eb3 --- /dev/null +++ b/animal.cpp @@ -0,0 +1,80 @@ +#include "game_objects.h" +#include +#include +#include "service.h" + +using namespace std; +using namespace sf; + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// HUMAN //////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + +Human::Human(b2World* World, Vector2f initCord, string textr, string name) :Alive(World, initCord, textr, name, rect_T, 10) { + setDrug(3, 2); +} + +Human::~Human() { +} + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// PERSON //////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + +Person::Person(b2World* World, Vector2f initCord, string textr, string name) :Human(World, initCord, textr, name) { + +} + + +void Person::blit() { + this->update(); + Human::blit(); +} + + +void Person::update() { + PAIForce(); +} + + +void Person::PAIForce() { + + if (PAIdelta < PAIiter) { + PAIdelta++; + body_ph->ApplyForceToCenter(PAIvec, true); + }else { + PAIiter = Rand::intRand(60,240); + PAIdelta = 0; + b2Vec2 temp(0, 0); + float x = Rand::intRand(0, 10), y = Rand::intRand(0, 10); + temp.Set(x,y); + temp.Normalize(); + if (Rand::intRand(0, 1)) { + PAIvec = -speed*temp; + }else{ + PAIvec = speed*temp; + } + } +} + + +Person::~Person() { + +} + + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// PLAYER //////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + +Player::Player(b2World* World, Vector2f initCord, string textr, string name) :Human(World, initCord, textr, name) { + speed = 2; +} + + +void Player::blit(){ + Human::blit(); +} + +Player::~Player() { +} diff --git a/baseObjects.cpp b/baseObjects.cpp new file mode 100644 index 0000000..e62d7fd --- /dev/null +++ b/baseObjects.cpp @@ -0,0 +1,256 @@ +#include "game_objects.h" +#include +#include +#include "phisic.h" +#include "log.h" +#include "window.h" +using namespace std; +using namespace sf; + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// BASE OBJECT /////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + + +BaseObject::BaseObject(Vector2f initCord, string textr) { + if (!texture.loadFromFile(textr)) { + Log::error("Texture load in BaseObject from " + textr, true); + } + texture.setSmooth(true); + g_body.setTexture(texture); + g_body.setOrigin(Vector2f(texture.getSize().x/2, texture.getSize().y/2)); + g_body.setPosition(initCord); +} + + +void BaseObject::updateTextrPosition(Vector2f newCord, float newAngle) { + g_body.setPosition(newCord); + g_body.setRotation(newAngle); +} + + +void BaseObject::blit() { + + if (isVisible) { + spl::ToDraw draw = { &g_body, depthRender + 1 }; + spl::Window::allDrawable.push_back(draw); + } +} + +BaseObject::~BaseObject() { + +} + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// DYNAMIC OBJECT //////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + +DynamicObject::DynamicObject(b2World* World, Vector2f initCord, string textr, string name, figureType type, float figureSize, bool isSensor):BaseObject(initCord, textr){ + + this->name = name; + + if ((texture.getSize().x - texture.getSize().y) && type != 3) + Log::error("Texture " + textr + " have to be square."); + + b2BodyDef b_bdef; + b_bdef.type = b2_dynamicBody; + b_bdef.linearDamping = 1; + b_bdef.angularDamping = 1; + b_bdef.position.Set(initCord.x / SCALE_BOX, initCord.y / SCALE_BOX); + body_ph = World->CreateBody(&b_bdef); + + b2FixtureDef b_fixture; + b2PolygonShape b_shape; + b2CircleShape c_shape; + b2Vec2 vec[6]; + float x = texture.getSize().x; + float y = texture.getSize().y; + switch (type) { + case 1:{ + c_shape.m_radius = figureSize / SCALE_BOX; + b_fixture.shape = &c_shape; + break; + } + case 2: { + vec[0].Set(0, -figureSize); + vec[1].Set(figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + vec[2].Set(-figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + for (int i = 0; i < 3; i++) { + vec[i].x = vec[i].x / SCALE_BOX; + vec[i].y = vec[i].y / SCALE_BOX; + } + b_shape.Set(vec, 3); + b_fixture.shape = &b_shape; + break; + } + case 3: { + vec[0].Set(-x/2+figureSize,y/2-figureSize); + vec[1].Set(-x / 2 + figureSize, -y / 2 + figureSize); + vec[2].Set(x / 2 - figureSize, -y / 2 + figureSize); + vec[3].Set(x / 2 - figureSize, y / 2 - figureSize); + for (int i = 0; i < 4; i++) { + vec[i].x = vec[i].x / SCALE_BOX; + vec[i].y = vec[i].y / SCALE_BOX; + } + b_shape.Set(vec, 4); + b_fixture.shape = &b_shape; + break; + } + case 4: { + vec[0].Set(0, figureSize); + vec[1].Set(-figureSize/ sqrt(1 + tan(18.f / DEG_BOX)*tan(18.f / DEG_BOX)), (tan(18.f / DEG_BOX)*figureSize)/ sqrt(1 + tan(18.f / DEG_BOX)*tan(18.f / DEG_BOX))); + vec[2].Set(-figureSize / sqrt(1 + tan(54.f / DEG_BOX)*tan(54.f / DEG_BOX)), (-tan(54.f / DEG_BOX)*figureSize) / sqrt(1 + tan(54.f / DEG_BOX)*tan(54.f / DEG_BOX))); + vec[3].Set(figureSize / sqrt(1 + tan(54.f / DEG_BOX)*tan(54.f / DEG_BOX)), (-tan(54.f / DEG_BOX)*figureSize) / sqrt(1 + tan(54.f / DEG_BOX)*tan(54.f / DEG_BOX))); + vec[4].Set(figureSize / sqrt(1 + tan(18.f / DEG_BOX)*tan(18.f / DEG_BOX)), (tan(18.f / DEG_BOX)*figureSize) / sqrt(1 + tan(18.f / DEG_BOX)*tan(18.f / DEG_BOX))); + for (int i = 0; i < 5; i++) { + vec[i].x = vec[i].x / SCALE_BOX; + vec[i].y = vec[i].y / SCALE_BOX; + } + b_shape.Set(vec, 5); + b_fixture.shape = &b_shape; + break; + } + case 5: { + vec[0].Set(0, figureSize); + vec[1].Set(-figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + vec[2].Set(-figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (-tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + vec[3].Set(0, -figureSize); + vec[4].Set(figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + vec[5].Set(figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (-tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + for (int i = 0; i < 6; i++) { + vec[i].x = vec[i].x / SCALE_BOX; + vec[i].y = vec[i].y / SCALE_BOX; + } + b_shape.Set(vec, 6); + b_fixture.shape = &b_shape; + break; + } + default: + Log::error("No such type (figure type) in DynamicObject"); + } + + b_fixture.isSensor = isSensor; + b_fixture.density = 1; + body_ph->CreateFixture(&b_fixture); + body_ph->SetUserData(this); +} + + +void DynamicObject::setDrug(float linear, float angular) { + body_ph->SetLinearDamping(linear); + body_ph->SetAngularDamping(angular); +} + +void DynamicObject::update() { + updateTextrPosition(Vector2f(body_ph->GetPosition().x*SCALE_BOX, body_ph->GetPosition().y*SCALE_BOX), body_ph->GetAngle()*DEG_BOX); +} + +void DynamicObject::blit() { + update(); + if (isVisible) { + spl::ToDraw draw = { &g_body, depthRender }; + spl::Window::allDrawable.push_back(draw); + } +} + +DynamicObject::~DynamicObject() { + +} + + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// STATIC OBJECT //////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + +StaticObject::StaticObject(b2World* World, Vector2f initCord,float angle , string textr, string name, figureType type, float figureSize, bool isSensor) :BaseObject(initCord, textr) { + + this->name = name; + + if ((texture.getSize().x - texture.getSize().y) && type != 3) + Log::error("Texture " + textr + " have to be square."); + + b2BodyDef b_bdef; + b_bdef.type = b2_staticBody; + b_bdef.position.Set(initCord.x / SCALE_BOX, initCord.y / SCALE_BOX); + body_ph = World->CreateBody(&b_bdef); + + b2FixtureDef b_fixture; + b2PolygonShape b_shape; + b2CircleShape c_shape; + b2Vec2 vec[6]; + float x = texture.getSize().x; + float y = texture.getSize().y; + switch (type) { + case 1: { + c_shape.m_radius = figureSize / SCALE_BOX; + b_fixture.shape = &c_shape; + break; + } + case 2: { + vec[0].Set(0, -figureSize); + vec[1].Set(figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + vec[2].Set(-figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + for (int i = 0; i < 3; i++) { + vec[i].x = vec[i].x / SCALE_BOX; + vec[i].y = vec[i].y / SCALE_BOX; + } + b_shape.Set(vec, 3); + b_fixture.shape = &b_shape; + break; + } + case 3: { + vec[0].Set(-x / 2 + figureSize, y / 2 - figureSize); + vec[1].Set(-x / 2 + figureSize, -y / 2 + figureSize); + vec[2].Set(x / 2 - figureSize, -y / 2 + figureSize); + vec[3].Set(x / 2 - figureSize, y / 2 - figureSize); + for (int i = 0; i < 4; i++) { + vec[i].x = vec[i].x / SCALE_BOX; + vec[i].y = vec[i].y / SCALE_BOX; + } + b_shape.Set(vec, 4); + b_fixture.shape = &b_shape; + break; + } + case 4: { + vec[0].Set(0, figureSize); + vec[1].Set(-figureSize / sqrt(1 + tan(18.f / DEG_BOX)*tan(18.f / DEG_BOX)), (tan(18.f / DEG_BOX)*figureSize) / sqrt(1 + tan(18.f / DEG_BOX)*tan(18.f / DEG_BOX))); + vec[2].Set(-figureSize / sqrt(1 + tan(54.f / DEG_BOX)*tan(54.f / DEG_BOX)), (-tan(54.f / DEG_BOX)*figureSize) / sqrt(1 + tan(54.f / DEG_BOX)*tan(54.f / DEG_BOX))); + vec[3].Set(figureSize / sqrt(1 + tan(54.f / DEG_BOX)*tan(54.f / DEG_BOX)), (-tan(54.f / DEG_BOX)*figureSize) / sqrt(1 + tan(54.f / DEG_BOX)*tan(54.f / DEG_BOX))); + vec[4].Set(figureSize / sqrt(1 + tan(18.f / DEG_BOX)*tan(18.f / DEG_BOX)), (tan(18.f / DEG_BOX)*figureSize) / sqrt(1 + tan(18.f / DEG_BOX)*tan(18.f / DEG_BOX))); + for (int i = 0; i < 5; i++) { + vec[i].x = vec[i].x / SCALE_BOX; + vec[i].y = vec[i].y / SCALE_BOX; + } + b_shape.Set(vec, 5); + b_fixture.shape = &b_shape; + break; + } + case 5: { + vec[0].Set(0, figureSize); + vec[1].Set(-figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + vec[2].Set(-figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (-tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + vec[3].Set(0, -figureSize); + vec[4].Set(figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + vec[5].Set(figureSize / sqrt(1 + tan(PI / 6)*tan(PI / 6)), (-tan(PI / 6)*figureSize) / sqrt(1 + tan(PI / 6)*tan(PI / 6))); + for (int i = 0; i < 6; i++) { + vec[i].x = vec[i].x / SCALE_BOX; + vec[i].y = vec[i].y / SCALE_BOX; + } + b_shape.Set(vec, 6); + b_fixture.shape = &b_shape; + break; + } + default: + Log::error("No such type (figure type) in StaticObject"); + } + + b_fixture.isSensor = isSensor; + body_ph->CreateFixture(&b_fixture); + body_ph->SetTransform(body_ph->GetPosition(), angle/DEG_BOX); + body_ph->SetUserData(this); + updateTextrPosition(Vector2f(body_ph->GetPosition().x*SCALE_BOX, body_ph->GetPosition().y*SCALE_BOX), body_ph->GetAngle()*DEG_BOX); +} + +StaticObject::~StaticObject() { + +} diff --git a/gameObjects.cpp b/gameObjects.cpp new file mode 100644 index 0000000..9328b12 --- /dev/null +++ b/gameObjects.cpp @@ -0,0 +1,168 @@ +#include "game_objects.h" +#include +#include +#include "phisic.h" +#include "log.h" +#include "window.h" + +using namespace std; +using namespace sf; + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// HOUSE ////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + +House::House(b2World* World, Vector2f initCord, float angle, string textureHouse, string textureDoor, string name) :StaticObject(World, initCord, angle, textureHouse, name, rect_T, 10, false) { + depthRender = -100; + Vector2f doorCord(initCord); + doorCord.x = doorCord.x + ((texture.getSize().y)/2-10) * sin(angle / DEG_BOX); + doorCord.y = doorCord.y - ((texture.getSize().y)/2-10) * cos(angle / DEG_BOX); + door = new StaticObject(World, doorCord, angle,textureDoor,name+"_d",rect_T,4,true); + door->depthRender = depthRender - 1; +} + +void House::blit() { + door->blit(); + BaseObject::blit(); +} + +House::~House() { + delete door; +} + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// FIR_TREE //////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + + +Fir_tree::Fir_tree(b2World* World, sf::Vector2f initCord, std::string textr, std::string name) :StaticObject(World, initCord, 0, textr, name, tringle_T, 17, false) { +} + +bool Fir_tree::decreaseHelth(int delta){ + helth -= delta; + if (helth <= 0) + return 0; + return 1; +} + +void Fir_tree::setHelth(int helth) { + this->helth = helth; +} + +Fir_tree::~Fir_tree() { +} + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// SHRUB //////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + + +Shrub::Shrub(b2World* World, sf::Vector2f initCord, std::string textr, std::string name) :StaticObject(World, initCord, 0, textr, name, rect_T, 15, true) { + g_body.setColor(sf::Color(255, 255, 255, 175)); + depthRender = -200; +} + +bool Shrub::decreaseHelth(int delta) { + helth -= delta; + if (helth <= 0) + return 0; + return 1; +} + +void Shrub::setHelth(int helth) { + this->helth = helth; +} + +Shrub::~Shrub() { +} + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// ALIVE //////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + +Alive::Alive(b2World* World, sf::Vector2f initCord, std::string textr, std::string name, figureType type, float figureSize): DynamicObject(World, initCord, textr, name, type, figureSize, false) { + + if (!texture_zone.loadFromFile(textr, sf::IntRect(texture.getSize().x/2, texture.getSize().x / 2, 2, 2))) { + Log::error("Texture load in Alive. "); + } + texture_zone.setSmooth(true); + g_zone.setRadius(radiusZone); + g_zone.setFillColor(Color(230, 255, 255, 30)); + g_zone.setTexture(&texture_zone); + + b2CircleShape z_shape; + z_shape.m_radius = radiusZone / SCALE_BOX; + b2FixtureDef z_fixt; + z_fixt.isSensor = true; + z_fixt.shape = &z_shape; + zoneFixt = body_ph->CreateFixture(&z_fixt); +} + +void Alive::setRadius(float radius) { + if (radius > 0) { + radiusZone = radius; + } + updateRadiusZone(); +} + +void Alive::moveRadius(float radiusDelta) { + if (radiusZone + radiusDelta >= 1) + radiusZone += radiusDelta; + else + radiusZone = 1; + updateRadiusZone(); +} + +void Alive::setZoneVisible(bool isVisible) { + isVisibleZone = isVisible; +} + +void Alive::updateRadiusZone() { + g_zone.setRadius(radiusZone); + + body_ph->DestroyFixture(zoneFixt); + + b2CircleShape z_shape; + z_shape.m_radius = radiusZone / SCALE_BOX; + b2FixtureDef z_fixture; + z_fixture.isSensor = true; + z_fixture.shape = &z_shape; + zoneFixt = body_ph->CreateFixture(&z_fixture); +} + +void Alive::update() { + updateZonePos(Vector2f(body_ph->GetPosition().x*SCALE_BOX, body_ph->GetPosition().y*SCALE_BOX)); +} + +void Alive::updateZonePos(Vector2f cord) { + g_zone.setPosition(Vector2f(cord.x - radiusZone, cord.y - radiusZone)); +} + +void Alive::blit() { + update(); + DynamicObject::blit(); + + if (isVisibleZone) { + spl::ToDraw set_z = { &g_zone, depthRender + 1 }; + spl::Window::allDrawable.push_back(set_z); + } +} + +bool Alive::decreaseHelth(int delta) { + helth -= delta; + if (helth <= 0) + return 0; + return 1; +} + +void Alive::setHelth(int helth) { + this->helth = helth; +} + +Alive::~Alive() { +} + +////////////////////////////////////////////////////////////////////////////////////////////// +//////////// HUMAN //////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////// + diff --git a/game_objects.h b/game_objects.h new file mode 100644 index 0000000..39075a8 --- /dev/null +++ b/game_objects.h @@ -0,0 +1,203 @@ +#pragma once +#include +#include +#include + +enum figureType { + circle_T = 1, + tringle_T, + rect_T, + pentagon_T, + intnugon_T +}; + +class BaseObject { +protected: + sf::Texture texture; + sf::Sprite g_body; + + void updateTextrPosition(sf::Vector2f newCord, float newAngle); + bool isVisible = true; + +public: + int depthRender = 0; + + BaseObject(sf::Vector2f initCord, std::string texture); + + virtual void blit(); + + virtual ~BaseObject(); +}; + + +class DynamicObject :public BaseObject { +private: + void update(); + +protected: + + void setDrug(float linear, float angular); + +public: + + std::string name = "None"; + + b2Body *body_ph; + + //can be load only square texture. for rect сan be not square + //figureSize - radius of figure. for rect it`s shadow size + DynamicObject(b2World* World, sf::Vector2f initCord, std::string texture, std::string name, figureType type, float figureSize = 0, bool isSensor = false);//circle, tringle + + + + void blit(); + + ~DynamicObject(); +}; + + +class StaticObject :public BaseObject { +protected: + b2Body *body_ph; +public: + + std::string name = "None"; + + //can be load only square texture. for rect сan be not square + //figureSize - radius of figure. for rect it`s shadow size + //angle in degree. + StaticObject(b2World* World, sf::Vector2f initCord,float angle, std::string texture, std::string name, figureType type, float figureSize = 0, bool isSensor = false); + + ~StaticObject(); +}; + + +class House :public StaticObject { +public: + StaticObject *door; + + + //shadow size 10 + //door shadow 4 + House(b2World* World, sf::Vector2f initCord, float angle, std::string textureHouse, std::string textureDoor, std::string name); + + void blit(); + + ~House(); +}; + +class Fir_tree :public StaticObject { +private: + int helth = 100; +public: + + //texture 40x40. size - 17 + Fir_tree(b2World* World, sf::Vector2f initCord, std::string textr, std::string name); + + void setHelth(int helth); + bool decreaseHelth(int delta = 0); //1 - if health >= 0 + + ~Fir_tree(); +}; + + +class Shrub :public StaticObject { +private: + int helth = 100; +public: + + //sahdow size 15, rect trtigger + Shrub(b2World* World, sf::Vector2f initCord, std::string textr, std::string name); + + void setHelth(int helth); + bool decreaseHelth(int delta = 0); //1 - if health >= 0 + + ~Shrub(); +}; + + +class Alive :public DynamicObject { +private: + + b2Fixture *zoneFixt; + sf::Texture texture_zone; + sf::CircleShape g_zone; + void updateZonePos(sf::Vector2f cord); + + float radiusZone = 30.f; + bool isVisibleZone = true; + + void updateRadiusZone(); + + void update(); +protected: + + + int helth = 100; + +public: + + Alive(b2World* World, sf::Vector2f initCord, std::string textr, std::string name, figureType type, float figureSize); + + void setHelth(int helth); + bool decreaseHelth(int delta = 0); //1 - if health >= 0 + + void setRadius(float radius = 0); + void moveRadius(float radius_delta = 0); + void setZoneVisible(bool isVisible = true); + + void blit(); + + ~Alive(); + +}; + + +class Human :public Alive { +private: + +protected: + float speed = 0.5; +public: + + //Human texture have to be a single color with shadow(10 px) 32x32. + Human(b2World* World, sf::Vector2f initCord, std::string textr, std::string name); + + ~Human(); +}; + + +class Person : public Human { +private: + void PAIForce();//pseudo AI + void update(); + + b2Vec2 PAIvec; + int PAIdelta = 0; + int PAIiter = 0; + +protected: + +public: + Person(b2World* World, sf::Vector2f initCord, std::string textr, std::string name); + + void blit(); + + ~Person(); +}; + + +class Player : public Human { +private: + +protected: + +public: + + Player(b2World* World, sf::Vector2f initCord, std::string textr, std::string name); + + void blit(); + + ~Player(); +}; + diff --git a/image/house/door.png b/image/house/door.png new file mode 100644 index 0000000..94cc0f1 Binary files /dev/null and b/image/house/door.png differ diff --git a/image/house/door2.png b/image/house/door2.png new file mode 100644 index 0000000..30f5c3c Binary files /dev/null and b/image/house/door2.png differ diff --git a/image/house/house.png b/image/house/house.png new file mode 100644 index 0000000..14633ba Binary files /dev/null and b/image/house/house.png differ diff --git a/image/house/house2.png b/image/house/house2.png new file mode 100644 index 0000000..4dce28e Binary files /dev/null and b/image/house/house2.png differ diff --git a/image/human.png b/image/human/human1.png similarity index 100% rename from image/human.png rename to image/human/human1.png diff --git a/image/human/human2.png b/image/human/human2.png new file mode 100644 index 0000000..4304a7b Binary files /dev/null and b/image/human/human2.png differ diff --git a/image/player.png b/image/human/player.png similarity index 100% rename from image/player.png rename to image/human/player.png diff --git a/image/veget/shrub.png b/image/veget/shrub.png new file mode 100644 index 0000000..03ef6cd Binary files /dev/null and b/image/veget/shrub.png differ diff --git a/image/veget/shrub2.png b/image/veget/shrub2.png new file mode 100644 index 0000000..f6cbab3 Binary files /dev/null and b/image/veget/shrub2.png differ diff --git a/image/veget/tree.png b/image/veget/tree.png new file mode 100644 index 0000000..707f077 Binary files /dev/null and b/image/veget/tree.png differ diff --git a/log.cpp b/log.cpp index d4db62d..7b9eea6 100644 --- a/log.cpp +++ b/log.cpp @@ -63,14 +63,13 @@ ScreenLog::ScreenLog() { } } -void ScreenLog::setNewLog(string name, int id, int pos) { +void ScreenLog::setNewLog(string name, int id) { lgT st; st.name = name; st.id = id; st.text.setFont(font); st.text.setString(name + ": " + "None"); - st.pos = pos; st.text.setCharacterSize(15); st.text.setFillColor(sf::Color::White); st.text.setOutlineColor(sf::Color::Black); @@ -89,7 +88,7 @@ void ScreenLog::setValue(int id, string value) { void ScreenLog::blit() { for (int i = 0; i < logText.size(); i++) { - logText[i].text.setPosition(spl::Window::currGlobalViewCord.x + 5- spl::Window::currScreenSize.x/2, 15 * logText[i].pos + spl::Window::currGlobalViewCord.y- spl::Window::currScreenSize.y / 2); + logText[i].text.setPosition(spl::Window::currGlobalViewCord.x + 5- spl::Window::currScreenSize.x/2, 15 * logText[i].id + spl::Window::currGlobalViewCord.y- spl::Window::currScreenSize.y / 2); spl::ToDraw td = { &logText[i].text, -1000 }; spl::Window::allDrawable.push_back(td); } diff --git a/log.h b/log.h index ef515e3..306958e 100644 --- a/log.h +++ b/log.h @@ -29,7 +29,6 @@ class ScreenLog { int id = 0; string name; sf::Text text; - int pos = 0; }; static sf::Font font; @@ -38,7 +37,7 @@ class ScreenLog { public: ScreenLog(); - void setNewLog(string name,int id, int pos = 0); + void setNewLog(string name,int id = 0); static void setValue(int id, string value = "None"); diff --git a/main.cpp b/main.cpp index c8669d4..ed4c2e4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,9 @@ #include "window.h" -#include "human.h" #include "log.h" #include #include "phisic.h" #include +#include "game_objects.h" using namespace std; int main() { @@ -19,21 +19,30 @@ int main() { Log::log("____________LOG SESSION START__________", true); ScreenLog screenLog; - screenLog.setNewLog("FPS", 1, 0); - screenLog.setNewLog("collision h1", 2, 1); - screenLog.setNewLog("collision h2", 3, 2); - spl::Window window; + screenLog.setNewLog("FPS", 0); + screenLog.setNewLog("collision h1", 1); + screenLog.setNewLog("collision h2", 2); + screenLog.setNewLog("Mouse: ", 3); spl::Window window; + + Person h(&World, sf::Vector2f(605, 190), "image/human/human1.png","human1"); + Person h2(&World, sf::Vector2f(520, 354), "image/human/human2.png","human2"); + Player player(&World, sf::Vector2f(370, 235), "image/human/player.png","player"); - Human h(sf::Vector2f(250, 250), "image/human.png","human1", &World); - Human h2(sf::Vector2f(200, 100), "image/human.png","human2", &World); - Human player(sf::Vector2f(150, 150), "image/player.png","player", &World); - player.depthRender = -10; + House house(&World, sf::Vector2f(150, 100), -120,"image/house/house2.png", "image/house/door2.png", "house"); + House house1(&World, sf::Vector2f(300, 400), 30, "image/house/house.png", "image/house/door.png", "house1"); + House house2(&World, sf::Vector2f(500, 100), 120, "image/house/house.png", "image/house/door.png", "house2"); + Fir_tree y1(&World, sf::Vector2f(350, 175), "image/veget/tree.png", "y1"); + Fir_tree y2(&World, sf::Vector2f(127, 419), "image/veget/tree.png", "y2"); + Fir_tree y3(&World, sf::Vector2f(107, 316), "image/veget/tree.png", "y3"); + + Shrub s1(&World, sf::Vector2f(207, 250), "image/veget/shrub.png", "sh1"); + Shrub s2(&World, sf::Vector2f(397, 324), "image/veget/shrub2.png", "sh2"); + Shrub s3(&World, sf::Vector2f(492, 290), "image/veget/shrub2.png", "sh3"); - int i = 0; - sf::Clock clock; - float speed = 3; + int i = 0; + sf::Clock clock; float speed = 2; while (window.canvas.isOpen()){ sf::Event event; @@ -42,8 +51,7 @@ int main() { if (event.type == sf::Event::Closed) window.canvas.close(); } - - + if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { player.body_ph->ApplyForceToCenter(b2Vec2(0, -speed), true); } @@ -62,6 +70,7 @@ int main() { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) { player.moveRadius(-1); } + window.view.setCenter(sf::Vector2f(player.body_ph->GetPosition().x*SCALE_BOX, player.body_ph->GetPosition().y*SCALE_BOX)); /////////////// FPS ////////////////// @@ -74,15 +83,25 @@ int main() { clock.restart(); /////////////////////////////// + ScreenLog::setValue(3, to_string(sf::Mouse::getPosition(window.canvas).x) + " | " + to_string(sf::Mouse::getPosition(window.canvas).y)); + World.Step(1 / 60.f, 8, 3); window.updateState(); - + house.blit(); + house1.blit(); + house2.blit(); + y1.blit(); + y2.blit(); + y3.blit(); h.blit(); h2.blit(); + s1.blit(); + s2.blit(); + s3.blit(); player.blit(); screenLog.blit(); window.drawAll(); - } +} Log::log("____________LOG SESSION END____________", true); return 0; } diff --git a/phisic.cpp b/phisic.cpp index 536b6ca..0952f84 100644 --- a/phisic.cpp +++ b/phisic.cpp @@ -1,16 +1,16 @@ #include "phisic.h" #include "log.h" -#include "human.h" +#include "game_objects.h" void CollideListener::BeginContact(b2Contact* contact) { void *dataA = contact->GetFixtureA()->GetBody()->GetUserData(); void *dataB = contact->GetFixtureB()->GetBody()->GetUserData(); if(((static_cast(dataA)->name == "player") && (static_cast(dataB)->name == "human1"))||((static_cast(dataB)->name == "player") && (static_cast(dataA)->name == "human1"))) - ScreenLog::setValue(2, "YES"); + ScreenLog::setValue(1, "YES"); if (((static_cast(dataA)->name == "player") && (static_cast(dataB)->name == "human2")) || ((static_cast(dataB)->name == "player") && (static_cast(dataA)->name == "human2"))) - ScreenLog::setValue(3, "YES"); + ScreenLog::setValue(2, "YES"); } @@ -19,8 +19,8 @@ void CollideListener::EndContact(b2Contact* contact) { void *dataB = contact->GetFixtureB()->GetBody()->GetUserData(); if (((static_cast(dataA)->name == "player") && (static_cast(dataB)->name == "human1")) || ((static_cast(dataB)->name == "player") && (static_cast(dataA)->name == "human1"))) - ScreenLog::setValue(2, "NO"); + ScreenLog::setValue(1, "NO"); if (((static_cast(dataA)->name == "player") && (static_cast(dataB)->name == "human2")) || ((static_cast(dataB)->name == "player") && (static_cast(dataA)->name == "human2"))) - ScreenLog::setValue(3, "NO"); + ScreenLog::setValue(2, "NO"); } \ No newline at end of file diff --git a/phisic.h b/phisic.h index 19886ee..06abffb 100644 --- a/phisic.h +++ b/phisic.h @@ -6,6 +6,7 @@ static const float SCALE_BOX = 30.0f; //divide by this to get from pixel -> meter static const float DEG_BOX = 57.29577f; //divide by this to get from degree -> radian +static const float PI = 3.141592; class CollideListener : public b2ContactListener { public: diff --git a/service.cpp b/service.cpp new file mode 100644 index 0000000..bd2990a --- /dev/null +++ b/service.cpp @@ -0,0 +1,10 @@ +#include "service.h" +#include +#include + +unsigned int Rand::intRand(int min, int max) { + std::mt19937 rng; + rng.seed(std::random_device()()); + std::uniform_int_distribution dist6(min, max); + return (int)dist6(rng); +} \ No newline at end of file diff --git a/service.h b/service.h new file mode 100644 index 0000000..53f58cc --- /dev/null +++ b/service.h @@ -0,0 +1,6 @@ +#pragma once + +class Rand { +public: + static unsigned int intRand(int by, int to); // [by, to] +}; \ No newline at end of file diff --git a/simpllight.vcxproj b/simpllight.vcxproj new file mode 100644 index 0000000..a19170a --- /dev/null +++ b/simpllight.vcxproj @@ -0,0 +1,160 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {7A5EB4C8-ED57-4A33-9D75-7A9BEC697D58} + simpllight + 10.0.14393.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + C:\Game_Lib\SFML_pack\include;C:\Game_Lib\Box2d\include;%(AdditionalIncludeDirectories) + SFML_DYNAMIC;%(PreprocessorDefinitions) + + + C:\Game_Lib\SFML_pack\lib;C:\Game_Lib\Box2d\lib;%(AdditionalLibraryDirectories) + sfml-audio-d.lib;sfml-system-d.lib;sfml-window-d.lib;sfml-graphics-d.lib;Box2D.lib;%(AdditionalDependencies) + + + + + Level3 + Disabled + true + + + + + Level3 + MaxSpeed + true + true + true + + + true + true + + + + + Level3 + MaxSpeed + true + true + true + + + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/simpllight.vcxproj.filters b/simpllight.vcxproj.filters index 2eeee8d..2b064c1 100644 --- a/simpllight.vcxproj.filters +++ b/simpllight.vcxproj.filters @@ -22,9 +22,30 @@ {6cfda5ff-d353-4063-a229-f01de2093f50} + + {6ccf13cd-1228-4c9f-b01b-9b5f77306fc1} + + + {9bc2a2cd-9d02-4c1c-adf1-b824f0cdfeda} + + + {afe2a5ca-576f-4eaa-b40f-fc2af164803f} + + + {3896e636-3256-477a-9b29-cc986140af10} + + + {ea57235f-5e34-44ad-a6e5-6e12cc0126eb} + + + {546684ec-1712-43c9-ab85-a5f61842245f} + - + + Header Files + + Header Files @@ -33,33 +54,13 @@ Header Files - + Header Files - + Header Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files\graphic - - Resource Files\font @@ -78,11 +79,64 @@ - - Resource Files\image + + Resource Files\image\house + + + Resource Files\image\house + + + Resource Files\image\house + + + Resource Files\image\house + + + Resource Files\image\human + + + Resource Files\image\human + + + Resource Files\image\human - - Resource Files\image + + Resource Files\image\veget + + Resource Files\image\veget + + + Resource Files\image\veget + + + + + Source Files\game_objects + + + Source Files\game_objects + + + Source Files\game_objects + + + Source Files\graphic + + + Source Files\phisic + + + Source Files\service + + + Source Files\service + + + Source Files + + + Source Files + \ No newline at end of file diff --git a/state.cpp b/state.cpp index f364541..441423f 100644 --- a/state.cpp +++ b/state.cpp @@ -8,3 +8,4 @@ std::string State::mode = "dev"; sf::Color State::mainColorMajor(5,20,20); +sf::Color State::mainColorMinor(255, 0, 153); diff --git a/state.h b/state.h index 4fd22fc..96ac98f 100644 --- a/state.h +++ b/state.h @@ -15,5 +15,7 @@ class State { static std::string mode; //dev, game static sf::Color mainColorMajor; + + static sf::Color mainColorMinor; }; -#endif +#endif \ No newline at end of file diff --git a/window.cpp b/window.cpp index d0e2735..b04a58f 100644 --- a/window.cpp +++ b/window.cpp @@ -21,7 +21,7 @@ Window::Window() { view.setSize(sf::VideoMode().getDesktopMode().width, sf::VideoMode().getDesktopMode().height); view.setCenter(sf::VideoMode().getDesktopMode().width/2, sf::VideoMode().getDesktopMode().height/2); } - canvas.setFramerateLimit(60); + canvas.setFramerateLimit(120); canvas.setView(view); }