Kopiuj
#include "Data.hpp"
#include "func.h"
#include <fstream>
void inline DataInitialization() {
ConfigurationInitialization();
TextureInitilization();
GUIInitialization();
}
void inline ConfigurationInitialization() {
UIStatus = Standard;
LevelStarted = false;
TextLanguage = EN;
}
void inline TextureInitilization() {
ButtonTex.loadFromFile("textures/GUI/Button.png");
ButtonMouseOnTex.loadFromFile("textures/GUI/ButtonMouseOn.png");
SmallButtonTex.loadFromFile("textures/GUI/SmallButton.png");
SmallButtonMouseOnTex.loadFromFile("textures/GUI/SmallButtonMouseOn.png");
MainMenuBackgroundTex.loadFromFile("textures/GUI/MainMenuBackground.png");
A1tex.NoThurst.loadFromFile("textures/ships/A-1 nothurst.png");
}
void inline GUIInitialization() {
MainMenuBackground.setTexture(MainMenuBackgroundTex);
MainMenuBackground.setPosition(sf::Vector2f(0, 0));
MainMenuBackground.setScale(3, 3);
MainMenuBackground.setOrigin(180, 120);
NewGameButton = Button(ButtonTex, ButtonMouseOnTex, L"New game", sf::Vector2f(-450, -250), GlobalFont, 50, sf::Color(0, 0, 0), sf::Color(255, 255, 255), NewGameButtonFunc);
LoadButton = Button(ButtonTex, ButtonMouseOnTex, L"Load", sf::Vector2f(-450, -125), GlobalFont, 50, sf::Color(0, 0, 0), sf::Color(255, 255, 255), LoadButtonFunc);
OptionsButton = Button(ButtonTex, ButtonMouseOnTex, L"Options", sf::Vector2f(-450, 0), GlobalFont, 50, sf::Color(0, 0, 0), sf::Color(255, 255, 255), OptionsButtonFunc);
CreatorsButton = Button(ButtonTex, ButtonMouseOnTex, L"Creators", sf::Vector2f(-450, 125), GlobalFont, 50, sf::Color(0, 0, 0), sf::Color(255, 255, 255), CreatorsButtonFunc);
ExitButton = Button(ButtonTex, ButtonMouseOnTex, L"Exit", sf::Vector2f(-450, 250), GlobalFont, 50, sf::Color(0, 0, 0), sf::Color(255, 255, 255), ExitButtonFunc);
MenuXButton = Button(SmallButtonTex, SmallButtonMouseOnTex, L"×", sf::Vector2f(450, -300), GlobalFont, 100, sf::Color(0, 0, 0), sf::Color(255, 255, 255), MenuXButtonFunc);
LanguageLeftButton = Button(SmallButtonTex, SmallButtonMouseOnTex, L"<", sf::Vector2f(-300, -250), GlobalFont, 70, sf::Color(0, 0, 0), sf::Color(255, 255, 255), LanguageLeftButtonFunc);
LanguageRightButton = Button(SmallButtonTex, SmallButtonMouseOnTex, L">", sf::Vector2f(0, -250), GlobalFont, 70, sf::Color(0, 0, 0), sf::Color(255, 255, 255), LanguageRightButtonFunc);
LanguageBackground.setFillColor(sf::Color(10, 10, 10));
LanguageBackground.setPosition(-204, -250);
LanguageBackground.setSize(sf::Vector2f(204, 96));
MainMenuText1.setFont(GlobalFont);
MainMenuText1.setPosition(sf::Vector2f(0, -250));
MainMenuText1.setScale(2, 2);
MainMenuText1.setFillColor(sf::Color(0, 128, 255));
MainMenuText2.setFont(GlobalFont);
MainMenuText2.setPosition(sf::Vector2f(200, -150));
MainMenuText2.setFillColor(sf::Color(0, 191, 230));
MainMenuText1.setString("War in space");
MainMenuText2.setString("First attack");
}
Kopiuj
#pragma once
#ifndef ENGINE_HPP_
#define ENGINE_HPP_
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\System.hpp>
#include <SFML\Audio.hpp>
#include <cmath>
enum Behavior {Wave, Kamikaze, Null, Slow, Random};
enum Language
{
PL, EN
};
enum UI {
Standard, Options, Creators
};
struct ShipTex
{
sf::Texture NoThurst;
sf::Texture Up;
sf::Texture Down;
sf::Texture Left;
sf::Texture Right;
};
sf::Vector2f centerText(const sf::Text& Text, const sf::Sprite& button);
double Angle_Between_Points(sf::Vector2f a, sf::Vector2f b);
template <typename T>
class GameObject : public sf::Drawable {
protected:
int HP;
T Texture;
sf::Sprite Sprite;
GameObject() { HP = 1;}
~GameObject() { delete this; }
GameObject(int hp, T textures) {
HP = hp;
Texture = textures;
}
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
target.draw(Sprite, states);
}
void collision(GameObject& target) {
HP -= target.getHP();
}
public:
int getHP() { return HP; }
};
class SpaceShip : public GameObject<ShipTex> {
private:
int Ammo;
int GunDamage;
int Rockets;
int RocketDamage;
int Speed;
int Fuel;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
target.draw(Sprite, states);
}
public:
SpaceShip(ShipTex textures, int hp, int ammo, int gunDamage, int rockets, int rocketDamage, int speed, int fuel) {
Texture = textures;
HP = hp;
Ammo = ammo;
GunDamage = gunDamage;
Rockets = rockets;
RocketDamage = rocketDamage;
Speed = speed;
Fuel = fuel;
}
SpaceShip(const SpaceShip& ship) {
Texture = ship.Texture;
HP = ship.HP;
Ammo = ship.Ammo;
GunDamage = ship.GunDamage;
Rockets = ship.Rockets;
RocketDamage = ship.RocketDamage;
Speed = ship.Speed;
Fuel = ship.Fuel;
}
SpaceShip() : Ammo(0), GunDamage(0), Rockets(0), RocketDamage(0), Speed(0), Fuel(0) {}
void setPosition(sf::Vector2f pos) {
Sprite.setPosition(pos);
}
void Move(bool Up, bool Down, bool Right, bool Left) {
int x, y;
x = Sprite.getPosition().x;
y = Sprite.getPosition().y;
if (Up)
y -= Speed;
if (Down)
y += Speed;
if (Right)
x += Speed;
if (Left)
x -= Speed;
Sprite.setPosition(x, y);
}
};
class Button : public sf::Drawable {
private:
typedef void(*buttonFunc) ();
sf::Sprite ButtonBackground;
sf::Text Text;
sf::Texture StandardTexture;
sf::Texture MouseOnTexture;
sf::Color StandardTextColor;
sf::Color MouseOnTextColor;
buttonFunc ButtonFunc;
sf::Vector2f centerText(const sf::Text& Text, const sf::Sprite& button) {
sf::FloatRect textRect = Text.getLocalBounds();
return sf::Vector2f(button.getPosition().x + (button.getGlobalBounds().width - textRect.width) / 2.0f - textRect.left,
button.getPosition().y + (button.getGlobalBounds().height - textRect.height) / 2.0f - textRect.top);
}
void updateTextPosition() {
Text.setPosition(centerText(Text, ButtonBackground));
}
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
target.draw(ButtonBackground, states);
target.draw(Text, states);
}
public:
Button() {}
~Button() {};
template<typename F>
Button(sf::Texture& texture, sf::Texture & mouseOnTexture, std::wstring string, sf::Vector2f position, sf::Font& font, int characterSize,
sf::Color textColor, sf::Color mouseOnTextColor, F func) {
StandardTexture = texture;
MouseOnTexture = mouseOnTexture;
StandardTextColor = textColor;
MouseOnTextColor = mouseOnTextColor;
ButtonBackground.setTexture(StandardTexture);
ButtonBackground.setPosition(position);
ButtonBackground.setScale(3, 3);
ButtonBackground.setOrigin(0, 0);
Text.setString(string);
Text.setFont(font);
Text.setCharacterSize(characterSize);
Text.setFillColor(StandardTextColor);
updateTextPosition();
ButtonFunc = func;
}
void setButtonStandardTexture(sf::Texture& texture) {
StandardTexture = texture;
}
void setButtonMouseOnTexture(sf::Texture& texture) {
MouseOnTexture = texture;
}
void setTextures(sf::Texture& standardTexture, sf::Texture& mouseOnTexture) {
StandardTexture = standardTexture;
MouseOnTexture = mouseOnTexture;
}
void setPosition(sf::Vector2f position) {
ButtonBackground.setPosition(position);
updateTextPosition();
}
void setFont(sf::Font& font) {
Text.setFont(font);
updateTextPosition();
}
void setCharacterSize(int size) {
Text.setCharacterSize(size);
updateTextPosition();
}
void setString(std::wstring string) {
Text.setString(string);
updateTextPosition();
}
void setTextStandardColor(sf::Color color) {
StandardTextColor = color;
}
void setTextMouseOnColor(sf::Color color) {
MouseOnTextColor = color;
}
void setTextColors(sf::Color standardColor, sf::Color mouseOnColor) {
StandardTextColor = standardColor;
MouseOnTextColor = mouseOnColor;
}
sf::Vector2f getPosition() const {
return ButtonBackground.getPosition();
}
int getCharacterSize() const {
return Text.getCharacterSize();
}
std::wstring getString() const {
Text.getString();
}
bool isMouseOn(sf::Vector2f mousePos) {
if (mousePos.x > ButtonBackground.getPosition().x &&
mousePos.x < ButtonBackground.getPosition().x + ButtonBackground.getGlobalBounds().width &&
mousePos.y > ButtonBackground.getPosition().y &&
mousePos.y < ButtonBackground.getPosition().y + ButtonBackground.getGlobalBounds().height) {
ButtonBackground.setTexture(MouseOnTexture);
Text.setFillColor(MouseOnTextColor);
return true;
}
else {
ButtonBackground.setTexture(StandardTexture);
Text.setFillColor(StandardTextColor);
return false;
}
}
};
#endif
Projekt jest w trakcie przebudowy, więc mogły pozostać w nim resztki starych, tymczasowych rozwiązań.