diff --git a/BASE.cpp b/BASE.cpp new file mode 100644 index 0000000..2e33bbb --- /dev/null +++ b/BASE.cpp @@ -0,0 +1,102 @@ +#include "Building.hpp" + + +sf::Sprite* BASE::draw() +{ + if(Selected) + { + Sprite2.SetPosition(Pos); + return &Sprite2; + } + + Sprite.SetPosition(Pos); + return &Sprite; +} + + + +bool BASE::Hit(BASE* Obj) +{ + if(Test(Pos, Obj->Pos, RB, Obj->RB)) + { + return true; + } + + return false; +} + + +bool BASE::Test(sf::Vector2f A, sf::Vector2f B, sf::Vector2f RBA, sf::Vector2f RBB) +{ + if((A.x > B.x) && (A.x < RBB.x) && (A.y > B.y) && (A.y < RBB.y)) + { + /* if(A.x > B.x) + { + std::cout << " A.x Greater Then B.x\n"; + } + if(A.x < RBB.x) + { + std::cout << " A.x Less Then RBB.x\n"; + } + + if(A.y > B.y) + { + std::cout << " A.y Greater Then B.y\n"; + } + if(A.y < RBB.y) + { + std::cout << " A.y Less Then RBB.y\n"; + } */ + + + return true; + } + + if((RBA.x > B.x) && (RBA.x < RBB.x) && (RBA.y > B.y) && (RBA.y < RBB.y)) + { + /* if(RBA.x > B.x) + { + std::cout << " RBA.x Greter Then B.x\n"; + } + if(RBA.x < RBB.x) + { + std::cout << " RBA.x Less Then RBB.x\n"; + } + + if(RBA.y > B.y) + { + std::cout << " RBA.y Greater Then B.y\n"; + } + if(RBA.y < RBB.y) + { + std::cout << " RBA.y Less Then RBB.y\n"; + } */ + + + return true; + } + + return false; +} + + +std::string BASE::GetData() +{ + return "Hello"; +} + + +std::string BASE::TYPE() +{ + switch(Type) + { + case HUMAN: + return " Human "; + case ZOMBIE: + return " Zombie "; + case TER: + return " Building "; + default: + return " NULL "; + } +} diff --git a/Building.cpp b/Building.cpp new file mode 100644 index 0000000..1792224 --- /dev/null +++ b/Building.cpp @@ -0,0 +1 @@ +#include "Building.hpp" diff --git a/Building.hpp b/Building.hpp new file mode 100644 index 0000000..44da9e7 --- /dev/null +++ b/Building.hpp @@ -0,0 +1,39 @@ +#include "Units.hpp" + +#ifndef __BUILDING__BUILDING + #define __BUILDING__BUILDING + +struct BUILDING: BASE +{ + private: + + public: + BUILDING(sf::Image* img, float X, float Y) + { + Img = img; + Type = 3; + + Pos.x = X; + Pos.y = Y; + + Right = Img->GetWidth(); + Bottom = Img->GetHeight(); + + RB.x = Right; + RB.y = Bottom; + + + Sprite.SetImage(*Img); + + //std::cout << "X: " << Pos.x << " Y: " << Pos.y << " Right: " << Right << " Bottom: " << Bottom << "\n"; + Sprite.SetPosition(Pos); + } + + //bool Hit(sf::Vector2f Pos); + +}; + + + + +#endif diff --git a/Enemy.cpp b/Enemy.cpp new file mode 100644 index 0000000..c526702 --- /dev/null +++ b/Enemy.cpp @@ -0,0 +1,134 @@ +#include "Enemy.hpp" + +int ENEMY::Enemy = 0; + +sf::Sprite* ENEMY::draw() +{ + if(Selected) + { + Sprite2.SetPosition(Pos); + return &Sprite2; + } + + return &Sprite; +} + + +std::string ENEMY::GetData() +{ + if(!Alive) + { + return "DEAD"; + } + + std::ostringstream Stream; + Stream << "LID: " << LID << "\nHit Points: " << HP << "\nTarget: " << Target; + + return Stream.str(); +} + + +bool ENEMY::RightClick(float X, float Y) +{ + if(X >= Pos.x && X <= Right && Y >= Pos.y && Y <= Bottom) + { + Targeted = true; + return true; + } + else + { + Targeted = false; + return false; + } +} + + + +int ENEMY::Attack(UNIT* Unit) +{ + if(!Alive) + { + return 0; + } + + if(!Unit->Alive) + { + if(Unit->LID == Target) + { + Target = -1; + } + + return 0; + } + + float NewRange = fabs(sqrt(pow(Unit->Pos.y, 2) + pow(Unit->Pos.x, 2)) - sqrt(pow(Pos.x, 2) + pow(Pos.y, 2))); + TRange = fabs(sqrt((pow(TargetLoc.y, 2) + pow(TargetLoc.x, 2))) - sqrt(pow(Pos.x, 2) + pow(Pos.y, 2))); + + if(Unit->Type == HUMAN) + { + if(Target == -1 && Unit->Alive == true) //If Enemy Has No Target AND The Unit Being Looked At Is Alive + { + Target = Unit->LID; + } + + if(Unit->LID == Target) //Chase Target + { + Dest = Unit->Pos; + TargetLoc = Unit->Pos; + } + } + + if(Test(Pos, Unit->Pos, RB, Unit->RB)) //Test For Impact With Target + { + if(Unit->Type == ZOMBIE) + { + MoveAway(Unit); + return 0; + } + + std::cout << LID << " Is Attacking " << Unit->LID << std::endl; + + int Roll = rand() % 4; + switch(Roll) + { + case 1: + Unit->Hit(); + Kills++; + std::cout << LID << " Hit: " << Unit->LID << "\n"; + + //Target = -1; + return 1; + + case 2: + //std::cout << "\nZombifying " << Id << "\n"; + //Target = -1; + return 3; + + default: + //std::cout << Unit->ID << " Got Away From " << Id << std::endl; + break; + } + } + + return 0; +} + + +void ENEMY::Hit() +{ + HP--; + + if(HP <= 0) + { + Die(); + } +} + + +void ENEMY::Die() +{ + Alive = false; + Selected = false; + Id--; + Enemy--; +} diff --git a/Enemy.hpp b/Enemy.hpp new file mode 100644 index 0000000..14d7bec --- /dev/null +++ b/Enemy.hpp @@ -0,0 +1,81 @@ +#include "Units.hpp" + +struct ENEMY : UNIT +{ + private: + + public: + ENEMY(sf::Image* img, int Z): Targeted(false) + { + scale = SCALE; + + Enemy++; + + HP = 2; + + LID = Id; //Id Is A Static Data Member + PID = Id; + Id++; //Increament Id Everytime Constructor Is Called + Alive = true; + Selected = false; + Mode = NORMAL; + Speed = .5; + + Kills = 0; + + Pos = sf::Vector2f(0,0); + + Img = img; //Inherited From Base Class + Type = Z; //Inherited From Base Class + + Time.Reset(); + Target = -1; + + /////*Generate Units Position Until It Is Within The Bounds Of The Screen*///// + do + { + Pos.x = rand() % 200; //Start Somewhere between 0 and 200 + Right = Pos.x + (25 * scale); + }while(Pos.x < 0 || Right > WIDTH); + + do + { + Pos.y = rand() % HEIGHT; + Bottom = Pos.y + (50 * scale); + }while(Pos.y < 0 || Bottom > HEIGHT); + + RB.x = Right; + RB.y = Bottom; + + Sprite.SetImage(*Img); + Sprite.SetScale(scale, scale); + Sprite.SetSubRect(sf::IntRect(0, 0, 24, 50)); + + Sprite2.SetImage(*Img); + Sprite2.SetScale(scale, scale); + Sprite2.SetSubRect(sf::IntRect(25, 0, 50, 50)); + + Sprite3.SetImage(*Img); + Sprite3.SetScale(scale, scale); + Sprite3.SetSubRect(sf::IntRect(51, 0, 75, 50)); + + + Sprite.SetPosition(Pos); + Sprite2.SetPosition(Pos); + Sprite3.SetPosition(Pos); + + Dest = Pos; + } + + sf::Sprite* draw(); + std::string GetData(); + bool RightClick(float X, float Y); + int Attack(UNIT* Unit); + void Hit(); + void Die(); + + bool Targeted; + sf::Sprite Sprite3; + + static int Enemy; +}; diff --git a/Fighter.cpp b/Fighter.cpp new file mode 100644 index 0000000..244c425 --- /dev/null +++ b/Fighter.cpp @@ -0,0 +1,258 @@ +#include "Fighter.hpp" + +int FIGHTER::Fighters = 0; + +int FIGHTER::Attack(UNIT* Unit) +{ + if(!Alive) //If You're Dead You Don't Get To Attack + { + return 0; + } + + if(Unit->LID == LID) //Don't Attack Yourself + { + return 0; + } + + if(Ammo <= 0) //If You're Out Of Ammo Run Away + { + Mode = RUN; + MoveAway(Unit); + return 0; + } + + if(Unit->Type == HUMAN) //If UNIT Is A Human Don't Shoot It + { + MoveAway(Unit); + return 0; + } + + if(!Unit->Alive) //If What You're Evaluating Is Not Alive Don't Shoot It + { + return 0; + } + + if(Target == -1 && Unit->Alive && Unit->Type == ZOMBIE) //If You Have No Target, Choose A New One + { + //std::cout << "Targeting " << Unit->PID << std::endl; + Target = Unit->LID; + TargetLoc = Unit->Pos; + } + + float NewRange = fabs(sqrt(pow(Unit->Pos.y, 2) + pow(Unit->Pos.x, 2)) - sqrt(pow(Pos.x, 2) + pow(Pos.y, 2))); + TRange = fabs(sqrt((pow(TargetLoc.y, 2) + pow(TargetLoc.x, 2))) - sqrt(pow(Pos.x, 2) + pow(Pos.y, 2))); + + if(NewRange < TRange) + { + Target = Unit->LID; + TargetLoc = Unit->Pos; + TRange = NewRange; + } + + if(!Loaded) //If Your Weapon Not Loaded Run Away + { + Mode = RUN; + MoveAway(Unit); + return 0; + } + + if(Unit->LID == Target) //If UNIT Is Your Current Target + { + if(!Unit->Alive) + { + Target = -1; + return 0; + } + + if(TRange >= Range) //If UNIT Is Out Of 'Range' Left Or Right Approach Target + { + Dest = Unit->Pos; + TargetLoc = Unit->Pos; + + return 0; + } + + int Hit = rand() % 4; //If All Criteria Passed Then Fire + + std::cout << LID << " Is Shooting At A " << Unit->TYPE() << " At " << TRange << std::endl; + + Ammo--; + Time.Reset(); + Loaded = false; + Firing = true; + + switch(Hit) + { + case 1: + Unit->Hit(); + + //Target = -1; //One Hit Used To Kill The Opponent + return 1; + + default: + return 1; + } + } + + return 1; +} + + +std::string FIGHTER::GetData() +{ + if(!Alive) + { + return "DEAD"; + } + + std::ostringstream String; + + String << "LID: " << LID << "\nHit Points: " << HP << "\nTarget: " << Target << "\nAmmo " << Ammo; + + return String.str(); +} + + +void FIGHTER::Update() +{ + switch(Mode) //This Will Probably Be Eliminated + { + case WAIT: //If Unit Is Waiting Then Return To Normal Mode + Mode = NORMAL; + return; + + case CHASE: //If Unit Is In CHASE Mode They Will Not Choose A New Destination + Move(); + break; + + case NORMAL: + Move(); + break; + + case RUN: + Move(); + break; + + default: + break; + } + + if((Pos.x == Dest.x) && (Pos.y == Dest.y)) + { + NewDest(); + } + + if(Time.GetElapsedTime() >= 1) //Reload Timer NOTE: This Should NOT Be A Constant, SFML 2.0 Is Moving To Miliseconds + { + if(Ammo > 0) + { + Mode = NORMAL; + Loaded = true; + Time.Reset(); + } + } +} + +sf::Sprite* FIGHTER::draw() +{ + if(!Selected && Loaded) + { + Sprite.SetPosition(Pos); + return &Sprite; + } + + if(!Selected && Firing) + { + Firing = false; + Sprite2.SetPosition(Pos); + return &Sprite2; + } + + if(Selected && !Firing) + { + Sprite3.SetPosition(Pos); + return &Sprite3; + } + + if(Selected && Firing) + { + Firing = false; + Sprite4.SetPosition(Pos); + return &Sprite4; + } + + if(!Selected && !Loaded) + { + Sprite5.SetPosition(Pos); + return &Sprite5; + } + + if(Selected && !Loaded) + { + Sprite6.SetPosition(Pos); + return &Sprite6; + } +} + +inline bool FIGHTER::RightClick(float X, float Y) +{ + if(Selected) + { + Dest.x = X; + Dest.y = Y; + + return true; + } + + return false; +} + +void FIGHTER::Zombify(sf::Image* zimg) //Broken, This Function Not In Use +{ + std::cout << LID << " Zombified\n"; + + Type = ZOMBIE; + + Img = zimg; + Sprite.SetImage(*Img); +} + +void FIGHTER::Hit() +{ + HP--; + if(HP <= 0) + { + Die(); + } +} + + +void FIGHTER::Die() +{ + Alive = false; + Selected = false; + Id--; + Fighters--; +} + + +/* void FIGHTER::MoveAway(sf::Vector2f Obj) +{ + if(Obj.x > Pos.x) + { + Dest.x -= 1; + } + if(Obj.x < Pos.x) + { + Dest.x += 1; + } + + if(Obj.y > Pos.y) + { + Dest.y -= 1; + } + if(Obj.y < Pos.y) + { + Dest.y += 1; + } +} */ diff --git a/Fighter.hpp b/Fighter.hpp new file mode 100644 index 0000000..6ae4773 --- /dev/null +++ b/Fighter.hpp @@ -0,0 +1,102 @@ +#include "Units.hpp" + +struct FIGHTER : public UNIT +{ + private: + + public: + FIGHTER(sf::Image* img, sf::Image* HealthBar, int Z): Ammo(10), Loaded(true), Range(75), Firing(false) + { + scale = SCALE; + + Fighters++; + + Health = HealthBar; + HP = 5; + LID = Id; //Id Is A Static Data Member + PID = Id; + Id++; //Increament Id Everytime Constructor Is Called + Alive = true; + Selected = false; + Mode = NORMAL; + Speed = 1; + + Img = img; //Inherited From Base Class + Type = Z; //Inherited From Base Class + Target = -1; + + Time.Reset(); //Start Timer At Zero + + /////*Generate Units Position Until It Is Within The Bounds Of The Screen*///// + do + { + Pos.x = 400 + (rand() % 200); + Right = Pos.x + (25 * scale); + }while(Pos.x < 0 || Right > WIDTH); + + do + { + Pos.y = rand() % HEIGHT; + Bottom = Pos.y + (50 * scale); + }while(Pos.y < 0 || Bottom > HEIGHT); + + RB.x = Right; + RB.y = Bottom; + + Sprite.SetImage(*Img); + Sprite.SetScale(scale, scale); + Sprite.SetSubRect(sf::IntRect(0, 0, 24, 50)); + + Sprite2.SetImage(*Img); + Sprite2.SetScale(scale, scale); + Sprite2.SetSubRect(sf::IntRect(25, 0, 50, 50)); + + Sprite3.SetImage(*Img); + Sprite3.SetScale(scale, scale); + Sprite3.SetSubRect(sf::IntRect(51, 0, 75, 50)); + + Sprite4.SetImage(*Img); + Sprite4.SetScale(scale, scale); + Sprite4.SetSubRect(sf::IntRect(76, 0, 103, 50)); + + Sprite5.SetImage(*Img); + Sprite5.SetScale(scale, scale); + Sprite5.SetSubRect(sf::IntRect(0,51, 25, 100)); + + Sprite6.SetImage(*Img); + Sprite6.SetScale(scale, scale); + Sprite6.SetSubRect(sf::IntRect(52, 51, 76, 100)); + + Sprite.SetPosition(Pos); + Sprite2.SetPosition(Pos); + + Dest = Pos; + } + + void Update(); + //void MoveAway(sf::Vector2f Obj); + + sf::Sprite* draw(); + int Attack(UNIT* Unit); + std::string GetData(); + void Zombify(sf::Image* zimg); + void Hit(); + void Die(); + + void HealthImg(); + + bool RightClick(float X, float Y); + + float Range; + int Ammo; + bool Loaded; + bool Firing; + + static int Fighters; + + sf::Sprite Sprite3; + sf::Sprite Sprite4; + + sf::Sprite Sprite5; + sf::Sprite Sprite6; +}; diff --git a/Makefile.win b/Makefile.win new file mode 100644 index 0000000..aa646e4 --- /dev/null +++ b/Makefile.win @@ -0,0 +1,52 @@ +# Project: Project1 +# Compiler: Default GCC compiler +# Compiler Type: MingW 3 +# Makefile created by wxDev-C++ 7.3 on 20/03/12 17:56 + +WXLIBNAME = wxmsw28 +CPP = g++.exe +CC = gcc.exe +WINDRES = "windres.exe" +OBJ = Objects/MingW/Z.o Objects/MingW/Units.o Objects/MingW/Building.o Objects/MingW/BASE.o Objects/MingW/Fighter.o Objects/MingW/Enemy.o Objects/MingW/Portrait.o +LINKOBJ = "Objects/MingW/Z.o" "Objects/MingW/Units.o" "Objects/MingW/Building.o" "Objects/MingW/BASE.o" "Objects/MingW/Fighter.o" "Objects/MingW/Enemy.o" "Objects/MingW/Portrait.o" +LIBS = -L"C:/Program Files/Dev-Cpp/Lib" -L"C:/Program Files/Dev-Cpp/SFML-1.6-dev-windows-mingw/SFML-1.6/lib" ../../SFML-1.6-dev-windows-mingw/SFML-1.6/lib/libsfml-window.a ../../SFML-1.6-dev-windows-mingw/SFML-1.6/lib/libsfml-system.a ../../SFML-1.6-dev-windows-mingw/SFML-1.6/lib/libsfml-graphics.a -Wl,--enable-auto-import +INCS = -I"C:/Program Files/Dev-Cpp/Include" -I"C:/Program Files/Dev-Cpp/SFML-1.6-dev-windows-mingw/SFML-1.6/include" +CXXINCS = -I"C:/Program Files/Dev-Cpp/lib/gcc/mingw32/3.4.5/include" -I"C:/Program Files/Dev-Cpp/include/c++/3.4.5/backward" -I"C:/Program Files/Dev-Cpp/include/c++/3.4.5/mingw32" -I"C:/Program Files/Dev-Cpp/include/c++/3.4.5" -I"C:/Program Files/Dev-Cpp/include" -I"C:/Program Files/Dev-Cpp/" -I"C:/Program Files/Dev-Cpp/include/common/wx/msw" -I"C:/Program Files/Dev-Cpp/include/common/wx/generic" -I"C:/Program Files/Dev-Cpp/include/common/wx/html" -I"C:/Program Files/Dev-Cpp/include/common/wx/protocol" -I"C:/Program Files/Dev-Cpp/include/common/wx/xml" -I"C:/Program Files/Dev-Cpp/include/common/wx/xrc" -I"C:/Program Files/Dev-Cpp/include/common/wx" -I"C:/Program Files/Dev-Cpp/include/common" -I"C:/Program Files/Dev-Cpp/SFML-1.6-dev-windows-mingw/SFML-1.6/include" +RCINCS = --include-dir "C:/PROGRA~1/Dev-Cpp/include/common" +BIN = Output/MingW/Z.exe +DEFINES = +CXXFLAGS = $(CXXINCS) $(DEFINES) +CFLAGS = $(INCS) $(DEFINES) +GPROF = gprof.exe +RM = rm -f +LINK = g++.exe + +.PHONY: all all-before all-after clean clean-custom +all: all-before $(BIN) all-after + +clean: clean-custom + $(RM) $(LINKOBJ) "$(BIN)" + +$(BIN): $(OBJ) + $(LINK) $(LINKOBJ) -o "$(BIN)" $(LIBS) + +Objects/MingW/Z.o: $(GLOBALDEPS) Z.cpp ZEvent.hpp Units.hpp Fighter.hpp Units.hpp Enemy.hpp Units.hpp Building.hpp Units.hpp Portrait.hpp Units.hpp + $(CPP) -c Z.cpp -o Objects/MingW/Z.o $(CXXFLAGS) + +Objects/MingW/Units.o: $(GLOBALDEPS) Units.cpp Units.hpp + $(CPP) -c Units.cpp -o Objects/MingW/Units.o $(CXXFLAGS) + +Objects/MingW/Building.o: $(GLOBALDEPS) Building.cpp Building.hpp Units.hpp + $(CPP) -c Building.cpp -o Objects/MingW/Building.o $(CXXFLAGS) + +Objects/MingW/BASE.o: $(GLOBALDEPS) BASE.cpp Building.hpp Units.hpp + $(CPP) -c BASE.cpp -o Objects/MingW/BASE.o $(CXXFLAGS) + +Objects/MingW/Fighter.o: $(GLOBALDEPS) Fighter.cpp Fighter.hpp Units.hpp + $(CPP) -c Fighter.cpp -o Objects/MingW/Fighter.o $(CXXFLAGS) + +Objects/MingW/Enemy.o: $(GLOBALDEPS) Enemy.cpp Enemy.hpp Units.hpp + $(CPP) -c Enemy.cpp -o Objects/MingW/Enemy.o $(CXXFLAGS) + +Objects/MingW/Portrait.o: $(GLOBALDEPS) Portrait.cpp Portrait.hpp Units.hpp + $(CPP) -c Portrait.cpp -o Objects/MingW/Portrait.o $(CXXFLAGS) diff --git a/Output/MinGW/Sprites/Ammo.PNG b/Output/MinGW/Sprites/Ammo.PNG new file mode 100644 index 0000000..7671c3b Binary files /dev/null and b/Output/MinGW/Sprites/Ammo.PNG differ diff --git a/Output/MinGW/Sprites/Fighter2.PNG b/Output/MinGW/Sprites/Fighter2.PNG new file mode 100644 index 0000000..61b1297 Binary files /dev/null and b/Output/MinGW/Sprites/Fighter2.PNG differ diff --git a/Output/MinGW/Sprites/HealthBar.PNG b/Output/MinGW/Sprites/HealthBar.PNG new file mode 100644 index 0000000..0cb75d1 Binary files /dev/null and b/Output/MinGW/Sprites/HealthBar.PNG differ diff --git a/Output/MinGW/Sprites/Home.PNG b/Output/MinGW/Sprites/Home.PNG new file mode 100644 index 0000000..bab4deb Binary files /dev/null and b/Output/MinGW/Sprites/Home.PNG differ diff --git a/Output/MinGW/Sprites/Portrait.PNG b/Output/MinGW/Sprites/Portrait.PNG new file mode 100644 index 0000000..9fed4ec Binary files /dev/null and b/Output/MinGW/Sprites/Portrait.PNG differ diff --git a/Output/MinGW/Sprites/Zombie2.PNG b/Output/MinGW/Sprites/Zombie2.PNG new file mode 100644 index 0000000..f754d1f Binary files /dev/null and b/Output/MinGW/Sprites/Zombie2.PNG differ diff --git a/Output/MinGW/cour.ttf b/Output/MinGW/cour.ttf new file mode 100644 index 0000000..2c99e08 Binary files /dev/null and b/Output/MinGW/cour.ttf differ diff --git a/Portrait.cpp b/Portrait.cpp new file mode 100644 index 0000000..1136f15 --- /dev/null +++ b/Portrait.cpp @@ -0,0 +1,20 @@ +#include "Portrait.hpp" + +void PORTRAIT::Update(int Arg) +{ + switch(Arg) + { + case 5: Img->Copy(*Health, 0, 0, sf::IntRect(0, 0, 24, 4)); break; + case 4: Img->Copy(*Health, 0, 0, sf::IntRect(0, 4, 24, 7)); break; + case 3: Img->Copy(*Health, 0, 0, sf::IntRect(0, 8, 24, 11)); break; + case 2: Img->Copy(*Health, 0, 0, sf::IntRect(0, 12, 24, 15)); break; + case 1: Img->Copy(*Health, 0, 0, sf::IntRect(0, 16, 24, 19)); break; + case 0: Img->Copy(*Health, 0, 0, sf::IntRect(0, 20, 24, 23)); + } +} + + +void PORTRAIT::Update(UNIT* Unit) +{ + Update(Unit->HP); +} diff --git a/Portrait.hpp b/Portrait.hpp new file mode 100644 index 0000000..e5e7d8a --- /dev/null +++ b/Portrait.hpp @@ -0,0 +1,26 @@ +#include "Units.hpp" + + +struct PORTRAIT +{ + private: + + public: + PORTRAIT(sf::Image* img, sf::Image* health): Img(img), Health(health) + { + Img->Copy(*Health, 0, 0, sf::IntRect(0, 20, 24, 23)); + + Sprite.SetImage(*Img); + Sprite.SetPosition(0, 0); + } + + void Update(int Arg); + void Update(UNIT* Unit); + + sf::Image* Img; + sf::Image* Health; + + sf::Sprite Sprite; + + +}; diff --git a/README.md b/README.md new file mode 100644 index 0000000..543ec46 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Random +====== +This is a random SFML project by community member @ComputerGeek01 diff --git a/Random.zip b/Random.zip deleted file mode 100644 index e6241fd..0000000 Binary files a/Random.zip and /dev/null differ diff --git a/Units.cpp b/Units.cpp new file mode 100644 index 0000000..f6069d1 --- /dev/null +++ b/Units.cpp @@ -0,0 +1,289 @@ +#include "Units.hpp" + +int UNIT::Id = 0; + + +void UNIT::Set(float X, float Y) +{ + Pos.x = X; + Pos.y = Y; + + Right = Pos.x + (25 * scale); + Bottom = Pos.y + (50 * scale); + + RB.x = Right; + RB.y = Bottom; + + Sprite.SetPosition(Pos); +} + + +inline void UNIT::Die() +{ + Alive = false; + Selected = false; + Id--; + //std::cout << "Unit " << ID << " Has Died\n"; +} + + +void UNIT::Update() +{ + switch(Mode) + { + case WAIT: //If Unit Is Waiting Then Return To Normal Mode + Mode = NORMAL; + return; + + case CHASE: //If Unit Is In CHASE Mode They Will Not Choose A New Destination + Move(); + break; + + case NORMAL: + Move(); + break; + + default: + break; + } + + if((Pos.x == Dest.x) && (Pos.y == Dest.y)) + { + NewDest(); + } + + return; +} + +inline void UNIT::NewDest() +{ + Dest.x = rand() % WIDTH; + Dest.y = rand() % HEIGHT; +} + +void UNIT::Move() +{ + if(!Alive) + { + return; + } + + if(Dest.x > Pos.x) + { + Pos.x += Speed; + } + + if(Dest.x < Pos.x) + { + Pos.x -= Speed; + } + + + if(Dest.y > Pos.y) + { + Pos.y += Speed; + } + + if(Dest.y < Pos.y) + { + Pos.y -= Speed; + } + + if(Pos.x < 0) + { + Pos.x += Speed; + Dest.x = WIDTH; + } + if(Right > WIDTH) + { + Pos.x -= Speed; + Dest.x = 0; + } + + if(Pos.y < 0) + { + Pos.y += Speed; + Dest.y = HEIGHT; + } + if(Bottom > HEIGHT) + { + Pos.y -= Speed; + Dest.y = 0; + } + + Right = Pos.x + (25 * scale); + Bottom = Pos.y + (50 * scale); + + RB.x = Right; + RB.y = Bottom; + + Sprite.SetPosition(Pos); + + return; +} + + +inline void UNIT::Zombify(sf::Image* zimg) +{ + +} + + +void UNIT::Click(float X, float Y, bool Shift) +{ + if(X >= Pos.x && X <= Right && Y >= Pos.y && Y <= Bottom) + { + //std::cout << LID << std::endl; + Selected = true; + } + else + { + if(!Shift) + { + Selected = false; + } + } + return; +} + + +inline bool UNIT::RightClick(float X, float Y) +{ + +} + + +inline void UNIT::Go(float X, float Y) +{ + +} + + +inline void UNIT::Hit() +{ + +} + + +bool UNIT::Chase(UNIT* Unit) +{ + if(Unit->Type == Type) //If Units Are Of The Same Type Return False + { + return false; + } + if(!Unit->Alive) //If Target Is Dead Return False + { + Target = -1; + Mode = NORMAL; + return false; + } + + Dest = Unit->Pos; + + Mode = CHASE; //Unit Is Chasing + + return true; +} + + +void UNIT::MoveAway(UNIT* Unit) +{ + float NewRange = fabs(sqrt(pow(Unit->Pos.y, 2) + pow(Unit->Pos.x, 2)) - sqrt(pow(Pos.x, 2) + pow(Pos.y, 2))); + + //std::cout << PID << " Move Away From " << Unit->PID << std::endl; + + if(Test(Pos, Unit->Pos, RB, Unit->RB)) + { + if(Pos.x > Unit->Pos.x) + { + Dest.x = Pos.x + Speed; + } + if(Right < Unit->Right) + { + Dest.x = Pos.x - Speed; + } + + if(Pos.y > Unit->Pos.y) + { + Dest.y = Pos.y + Speed; + } + if(Bottom < Unit->Bottom) + { + Dest.y = Pos.y + Speed; + } + } + + Right = Pos.x + (25 * scale); + Bottom = Pos.y + (50 * scale); + + RB.x = Right; + RB.y = Bottom; +} + + +int UNIT::Attack(UNIT* Unit) {std::cout << "Do Nothing" << std::endl;} +/*{ + if(!Alive) + { + return 0; + } + + /*if(!Unit->Alive) + { + return 0; + } + + float XRange = fabs(Pos.x - Unit->Pos.x); + float YRange = fabs(Pos.y - Unit->Pos.y); + + if(Target == -1) + { + Target = Unit->PID; + } + + /*if(Unit->PID == Target) + { + if(!Unit->Alive) + { + Target = -1; + } + if(Target == Unit->PID) //Chase Target + { + Dest = Unit->Pos; + } + } + + if(Test(Pos, Unit->Pos, RB, Unit->RB)) //Test(...) Inherited From BASE Struct + { + if(Type == Unit->Type) + { + MoveAway(Unit); + return 0; + } + + //std::cout << ID << " Hit " << Unit->ID << "\n"; + int Roll = rand() % 3; + + switch(Roll) + { + case 1: + Unit->Die(); + Kills++; + std::cout << "\nHuman Killed: " << Unit->PID << "\n"; + Target = -1; + return 1; + + case 2: + std::cout << "\nZombifying " << PID << "\n"; + Target = -1; + return 3; + + default: + std::cout << Unit->PID << " Got Away From " << PID << std::endl; + break; + } + return 1; + } + + return 0; +}*/ diff --git a/Units.hpp b/Units.hpp new file mode 100644 index 0000000..18557fe --- /dev/null +++ b/Units.hpp @@ -0,0 +1,156 @@ +#ifndef __UNITS__UNITS + #define __UNITS__UNITS + +#include +#include +#include +#include +#include + +#include + +#define WIDTH 600 +#define HEIGHT 400 + +#define SCALE .75 + + +enum MODE{NORMAL, WAIT, CHASE, ATTACK, RUN}; +enum TYPE{HUMAN, ZOMBIE, TER}; + +struct BASE +{ + private: + + public: + BASE() {} + + virtual sf::Sprite* draw(); + bool Hit(BASE* Obj); + std::string TYPE(); + bool Test(sf::Vector2f A, sf::Vector2f B, sf::Vector2f RBA, sf::Vector2f RBB); + virtual std::string GetData(); + + int Type; + int LID; //Vector ID For Deletion And Looping + int PID; //Permanent ID + + sf::Image* Img; + + sf::Sprite Sprite; //Sprite Frame 1 + sf::Sprite Sprite2; //Sprite Frame 2 + + sf::Vector2f Pos; //Position Of The Sprite + sf::Vector2f RB; //Right Bottom Of The Sprite + + float Right; + float Bottom; + float Speed; + float scale; + + MODE Mode; + + bool Selected; +}; + +struct UNIT: BASE +{ + private: + + public: + UNIT(): Alive(true) {} + + UNIT(sf::Image* img, int Z): Kills(0) + { + scale = SCALE; + + LID = Id; //Id Is A Static Data Member + PID = Id; + Id++; //Increament Id Everytime Constructor Is Called + Alive = true; + Selected = false; + Mode = NORMAL; + Speed = 1; + + Pos = sf::Vector2f(0,0); + + Img = img; //Inherited From Base Class + Type = Z; //Inherited From Base Class + + Time.Reset(); + + /////*Generate Units Position Until It Is Within The Bounds Of The Screen*///// + do + { + Pos.x = rand() % 200; //Start Somewhere between 0 and 200 + Right = Pos.x + (25 * scale); + }while(Pos.x < 0 || Right > WIDTH); + + do + { + Pos.y = rand() % HEIGHT; + Bottom = Pos.y + (50 * scale); + }while(Pos.y < 0 || Bottom > HEIGHT); + + RB.x = Right; + RB.y = Bottom; + + Sprite.SetImage(*Img); + Sprite.SetScale(scale, scale); + Sprite.SetSubRect(sf::IntRect(0, 0, 24, 50)); + + Sprite2.SetImage(*Img); + Sprite2.SetScale(scale, scale); + Sprite2.SetSubRect(sf::IntRect(25, 0, 50, 50)); + + + Sprite.SetPosition(Pos); + Sprite2.SetPosition(Pos); + + Dest = Pos; + } + + void Set(float X, float Y); + + + int Hit(UNIT* Unit); + + virtual void Die(); + virtual void Update(); + virtual int Attack(UNIT* Unit); + virtual void Click(float X, float Y, bool Shift); + virtual void Go(float X, float Y); + virtual void Zombify(sf::Image* zimg); + virtual bool RightClick(float X, float Y); + virtual void Hit(); + + bool Chase(UNIT* Unit); + void Move(); + void MoveAway(UNIT* Unit); + void NewDest(); + + sf::Image* Health; + + static int Id; + + sf::Vector2f Dest; + sf::Vector2f TargetLoc; + + sf::Shape TargetLine; + + int Kills; + int Target; + int HP; + + bool Alive; + + float TRange; + + sf::Clock Time; +}; + + + + + +#endif diff --git a/Z.cpp b/Z.cpp new file mode 100644 index 0000000..13a8411 --- /dev/null +++ b/Z.cpp @@ -0,0 +1,180 @@ +#include + +#include + +#include "ZEvent.hpp" +#include "Units.hpp" +#include "Fighter.hpp" +#include "Enemy.hpp" +#include "Building.hpp" +#include "Portrait.hpp" + +void pause() __attribute__((destructor)); //This Is A GCC\Mingw Specific Function Attribute, I Have No Idea How To Do It Using "cl.exe" SOMEONE PLEASE TELL ME!!! + +void pause() +{ + std::cin.sync(); + std::cin.ignore(); +} + + +int main(int argc, char *argv[]) +{ + sf::RenderWindow App(sf::VideoMode(WIDTH, HEIGHT, 32), "SFML Zombies"); + App.SetFramerateLimit(90); + + std::vector UVec; + + srand(time(NULL)); + + std::string TEXT = "HELLO"; + + sf::Clock Clock; + sf::Font Font; + if(!Font.LoadFromFile("cour.ttf")) + { + std::cout << "FONT FAILED TO LOAD\n"; + } + sf::String Msg(TEXT, Font, 15); + Msg.Move(0, 0); + Msg.SetColor(sf::Color::Black); + + const sf::Input& Input = App.GetInput(); + sf::Event Event; + + float MX = 0; + float MY = 0; + + sf::Image HealthBar; + //HealthBar.LoadFromFile("C:\\Program Files\\Dev-Cpp\\SFML_Projects\\Sprites\\HealthBar.PNG"); + HealthBar.LoadFromFile("Sprites\\HealthBar.PNG"); + + sf::Image PortraitImg; + //PortraitImg.LoadFromFile("C:\\Program Files\\Dev-Cpp\\SFML_Projects\\Sprites\\Portrait.PNG"); + PortraitImg.LoadFromFile("Sprites\\Portrait.PNG"); + + sf::Image BaseImage; //Image That Workers Are Derived From + //BaseImage.LoadFromFile("C:\\Program Files\\Dev-Cpp\\SFML_Projects\\Sprites\\Fighter2.PNG"); + BaseImage.LoadFromFile("Sprites\\Fighter2.PNG"); + + sf::Image DWImg(BaseImage); //Default Worker Image + DWImg.CreateMaskFromColor(DWImg.GetPixel(3,3), 0); // Make The Color At This Point In The Image Transparent + DWImg.CreateMaskFromColor(DWImg.GetPixel(25,0), 0); //Hide The Pink Divider Between Frames + + sf::Image SWImg(BaseImage); //Currently Selected Worker Image + SWImg.CreateMaskFromColor(SWImg.GetPixel(3,3), 0); + + sf::Image ZImg; + //ZImg.LoadFromFile("C:\\Program Files\\Dev-Cpp\\SFML_Projects\\Sprites\\Zombie2.PNG"); + ZImg.LoadFromFile("Sprites\\Zombie2.PNG"); + ZImg.CreateMaskFromColor(ZImg.GetPixel(1,1), 0); + ZImg.CreateMaskFromColor(ZImg.GetPixel(25,0), 0); + + sf::Image HImg; + //HImg.LoadFromFile("C:\\Program Files\\Dev-Cpp\\SFML_Projects\\Sprites\\Home.PNG"); + HImg.LoadFromFile("Sprites\\Home.PNG"); + HImg.CreateMaskFromColor(HImg.GetPixel(1,1), 0); + + PORTRAIT Portrait(&PortraitImg, &HealthBar); + + BUILDING Home(&HImg, 10, 200); + + int FNum = 10; //Number of Fighters + int ZNum = 10; //Number of Zombies + + int Case = 0; + +///////*CREATING THE UNITS*/////// + + for(int i = 0; i < FNum; i++) + { + FIGHTER* Fighter = new FIGHTER(&DWImg, &HealthBar, HUMAN); + + UVec.push_back(Fighter); + } + + for(int i = 0; i < ZNum; i++) + { + ENEMY* Zombie = new ENEMY(&ZImg, ZOMBIE); + + UVec.push_back(Zombie); + } + +///////*THE RUNNING LOOP*/////// + + while(App.IsOpened()) + { + MX = Input.GetMouseX(); + MY = Input.GetMouseY(); + + App.Clear(sf::Color::White); + + while(App.GetEvent(Event)) + { + ZEvent(Event, &App); + } + + App.Draw(Home.Sprite); //Draw Buildings + + //Portrait.Update(1); //Portrait Is A Work In Progress + + for(int i = 0; i < UVec.size(); i++) //Update The Screen + { + UVec[i]->LID = i; + UVec[i]->Update(); + + if(UVec[i]->Selected) + { + Portrait.Update(UVec[i]); + Msg.SetText(UVec[i]->GetData()); + } + + if(Home.Hit(UVec[i])) // If Unit Would Hit Any Building In The Vector + { // + //UVec[i]->MoveAway(&Home);// Avoid The Building + } + + for(int j = 0; j < UVec.size(); j++) + { + if(UVec[i]->Attack(UVec[j]) == 3) + { + //UVec[i]->Zombify(&ZImg); //Not Implemented Yet + } + + if(Input.IsMouseButtonDown(sf::Mouse::Right)) + { + if(UVec[i]->Selected) + { + if(UVec[j]->RightClick(MX, MY)) + { + UVec[i]->Mode = CHASE; + UVec[i]->Target = UVec[j]->PID; + } + } + } + } + + if(Input.IsMouseButtonDown(sf::Mouse::Left)) + { + UVec[i]->Click(MX, MY, Input.IsKeyDown(sf::Key::LShift)); + } + + if(UVec[i]->Alive) + { + App.Draw(*UVec[i]->draw()); + } + else + { + //UVec.erase(UVec.begin() + UVec[i]->LID); //Not Worrying About This Right Now + } + } + + App.Draw(Msg); + //App.Draw(Portrait.Sprite); + App.Display(); + } + + UVec.erase(UVec.begin(), UVec.end()); //Hopefully Calling The Destructors Of The Objects In The Vector + + return EXIT_SUCCESS; +} diff --git a/Z.dev b/Z.dev new file mode 100644 index 0000000..e228cee --- /dev/null +++ b/Z.dev @@ -0,0 +1,215 @@ +[Project] +FileName=Z.dev +Name=Project1 +UnitCount=13 +PchHead=-1 +PchSource=-1 +Ver=3 +IsCpp=1 +ProfilesCount=2 +ProfileIndex=0 +Folders= + +[Unit1] +FileName=Z.cpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[VersionInfo] +Major=0 +Minor=1 +Release=1 +Build=1 +LanguageID=1033 +CharsetID=1252 +CompanyName= +FileVersion= +FileDescription= +InternalName= +LegalCopyright= +LegalTrademarks= +OriginalFilename= +ProductName= +ProductVersion= +AutoIncBuildNrOnRebuild=0 +AutoIncBuildNrOnCompile=0 + +[Profile1] +ProfileName=Mingw gcc 3.4.5 +Type=1 +ObjFiles= +Includes="C:\Program Files\Dev-Cpp\SFML-1.6-dev-windows-mingw\SFML-1.6\include" +Libs="C:\Program Files\Dev-Cpp\SFML-1.6-dev-windows-mingw\SFML-1.6\lib" +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker=../../SFML-1.6-dev-windows-mingw/SFML-1.6/lib/libsfml-window.a_@@_../../SFML-1.6-dev-windows-mingw/SFML-1.6/lib/libsfml-system.a_@@_../../SFML-1.6-dev-windows-mingw/SFML-1.6/lib/libsfml-graphics.a_@@__@@_-Wl,--enable-auto-import_@@_ +PreprocDefines= +CompilerSettings=0000000000000000000000 +Icon= +ExeOutput=Output\MingW +ObjectOutput=Objects\MingW +OverrideOutput=0 +OverrideOutputName=Z.exe +HostApplication= +CommandLine= +UseCustomMakefile=0 +CustomMakefile= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=0 +CompilerType=0 + +[Profile2] +ProfileName=MS VC++ 2005 +Type=1 +ObjFiles= +Includes= +Libs= +ResourceIncludes= +MakeIncludes= +Compiler= +CppCompiler= +Linker= +PreprocDefines= +CompilerSettings=000000000000010000000000000000000000 +Icon= +ExeOutput=Output\MSVC++2005 +ObjectOutput=Objects\MSVC++2005 +OverrideOutput=0 +OverrideOutputName= +HostApplication= +CommandLine= +UseCustomMakefile=0 +CustomMakefile= +IncludeVersionInfo=0 +SupportXPThemes=0 +CompilerSet=1 +CompilerType=1 + +[Unit2] +FileName=ZEvent.hpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit3] +FileName=Units.hpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit4] +FileName=Units.cpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit5] +FileName=Building.hpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit6] +FileName=Building.cpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit7] +FileName=BASE.cpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit8] +FileName=Fighter.cpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit9] +FileName=Enemy.cpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit10] +FileName=Portrait.hpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit11] +FileName=Portrait.cpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit12] +FileName=Fighter.hpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit13] +FileName=Enemy.hpp +CompileCpp=1 +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/Z.layout b/Z.layout new file mode 100644 index 0000000..a2e35d5 --- /dev/null +++ b/Z.layout @@ -0,0 +1,94 @@ +[Editors] +Focused=7 +Order=0,2,3,11,7,12,8,9,10,4,5,6,1 +[Editor_0] +Open=1 +Top=0 +CursorCol=37 +CursorRow=173 +TopLine=124 +LeftChar=1 +[Editor_1] +Open=1 +Top=0 +CursorCol=6 +CursorRow=15 +TopLine=1 +LeftChar=1 +[Editor_2] +Open=1 +Top=0 +CursorCol=9 +CursorRow=24 +TopLine=16 +LeftChar=1 +[Editor_3] +Open=1 +Top=0 +CursorCol=1 +CursorRow=164 +TopLine=144 +LeftChar=1 +[Editor_4] +Open=1 +Top=0 +CursorCol=13 +CursorRow=8 +TopLine=3 +LeftChar=1 +[Editor_5] +Open=1 +Top=0 +CursorCol=1 +CursorRow=2 +TopLine=1 +LeftChar=1 +[Editor_6] +Open=1 +Top=0 +CursorCol=17 +CursorRow=6 +TopLine=1 +LeftChar=1 +[Editor_7] +Open=1 +Top=1 +CursorCol=2 +CursorRow=231 +TopLine=192 +LeftChar=1 +[Editor_8] +Open=1 +Top=0 +CursorCol=19 +CursorRow=99 +TopLine=1 +LeftChar=1 +[Editor_9] +Open=1 +Top=0 +CursorCol=13 +CursorRow=6 +TopLine=1 +LeftChar=1 +[Editor_10] +Open=1 +Top=0 +CursorCol=62 +CursorRow=8 +TopLine=1 +LeftChar=1 +[Editor_11] +Open=1 +Top=0 +CursorCol=9 +CursorRow=6 +TopLine=1 +LeftChar=1 +[Editor_12] +Open=1 +Top=0 +CursorCol=9 +CursorRow=6 +TopLine=1 +LeftChar=1 diff --git a/ZEvent.hpp b/ZEvent.hpp new file mode 100644 index 0000000..64e8495 --- /dev/null +++ b/ZEvent.hpp @@ -0,0 +1,18 @@ +#ifndef __ZEvent__ZEvent + #define __ZEvent__ZEvent + +void ZEvent(sf::Event Event, sf::RenderWindow* APP) +{ + const sf::Input& Input = APP->GetInput(); + + if(Event.Key.Code == sf::Key::Escape) + { + if(Input.IsKeyDown(sf::Key::Escape)) + { + std::cout << "\nESCAPE PRESSED\n"; + APP->Close(); + } + } +} + +#endif