Untitled
unknown
plain_text
2 years ago
13 kB
25
Indexable
// main.cpp
#include "GameManager.h"
#include <Windows.h>
GameManager* gameManager = new GameManager();
int main()
{
#ifdef NDEBUG
HWND hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_HIDE);
#endif
gameManager->StartGameManager();
gameManager->Update();
delete gameManager;
return 0;
}
//tile.h
#pragma once
struct sTile {
enum TileType { None, Wall, Ghost, GhostHouse, Player, Snack };
bool isEmpty = true;
std::vector<TileType> tileTypes;
static int GetDistanceBetweenTiles(sf::Vector2i a, sf::Vector2i b)
{
return std::sqrt(std::pow(a.x - b.x, 2) +
std::pow(a.y - b.y, 2) * 1.0);
}
bool DoesTileHaveType(TileType type) {
for(TileType t : tileTypes)
{
if (type == t) return true;
}
return false;
}
bool DoesTileHaveOnlyType(TileType type) {
if (tileTypes.size() == 1) {
if (tileTypes[0] == Wall) return true;
}
return false;
}
void EraseTileType(TileType type) {
for (int i = 0; i < tileTypes.size(); i++){
if (tileTypes[i] == type)
tileTypes.erase(tileTypes.begin() + i);
}
}
};
// GameManager.h
#pragma once
#include "Common.h"
class State;
class GameManager
{
public:
sf::RenderWindow* window;
std::stack<State*> states;
float deltaTime = 0;
float aspectRatio;
void Update();
void StartGameManager();
~GameManager();
private:
sf::Clock clock;
};
// GameManager.cpp
#include "GameManager.h"
#include "States/MainMenuState/MainMenuState.h"
void GameManager::StartGameManager()
{
window = new sf::RenderWindow();
window->create(sf::VideoMode(1000, 1100), "Pacman", sf::Style::Fullscreen);
//window->create(sf::VideoMode::getFullscreenModes()[0], "Pacman", sf::Style::Resize);
aspectRatio = float(window->getSize().x) / float(window->getSize().y - 100);
sf::View v(sf::Vector2f(400, 450), sf::Vector2f(800 * aspectRatio, 900));
window->setView(v);
states.push(new MainMenuState(window, &states, this));
}
GameManager::~GameManager()
{
}
void GameManager::Update()
{
while (window->isOpen())
{
deltaTime = clock.restart().asSeconds();
//get input
sf::Event event;
while (window->pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window->close();
break;
case sf::Event::Resized:
aspectRatio = float(window->getSize().x) / float(window->getSize().y);
sf::View v(sf::Vector2f(400, 450), sf::Vector2f(800 * aspectRatio, 900));
window->setView(v);
break;
}
}
//handle states
if (!this->states.empty())
{
this->states.top()->Update(deltaTime);
if (this->states.top()->GetQuit())
{
this->states.top()->EndState();
delete this->states.top();
this->states.pop();
}
}
//Application end
else
{
this->window->close();
}
}
}
//common.h
#pragma once
#include <iostream>
#include <stdio.h>
#include <SFML/Graphics.hpp>
#include <stack>
//button.cpp
#include"Button.h"
Button::Button(float x, float y, float width, float height,
std::string text, unsigned character_size,
sf::Color text_idle_color, sf::Color text_hover_color, sf::Color text_active_color)
{
this->buttonState = BTN_IDLE;
this->shape.setPosition(sf::Vector2f(x, y));
this->shape.setSize(sf::Vector2f(width, height));
this->shape.setFillColor(sf::Color::Transparent);
font.loadFromFile("Fonts/Dosis-Light.ttf");
this->text.setFont(this->font);
this->text.setString(text);
this->text.setFillColor(text_idle_color);
this->text.setCharacterSize(character_size);
this->text.setPosition(
this->shape.getPosition().x + (this->shape.getGlobalBounds().width / 2.f) - this->text.getGlobalBounds().width / 2.f,
this->shape.getPosition().y + (this->shape.getGlobalBounds().height / 2.f) - this->text.getGlobalBounds().height / 1.5f
);
this->textIdleColor = text_idle_color;
this->textHoverColor = text_hover_color;
this->textActiveColor = text_active_color;
}
Button::~Button()
{
}
const bool Button::isPressed() const
{
if (this->buttonState == BTN_ACTIVE)
return true;
return false;
}
void Button::update(const sf::Vector2f mousePos)
{
//Idle
this->buttonState = BTN_IDLE;
//Hover
if (this->shape.getGlobalBounds().contains(mousePos))
{
this->buttonState = BTN_HOVER;
//Pressed
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
this->buttonState = BTN_ACTIVE;
}
}
switch (this->buttonState)
{
case BTN_IDLE:
this->text.setFillColor(this->textIdleColor);
break;
case BTN_HOVER:
this->text.setFillColor(this->textHoverColor);
break;
case BTN_ACTIVE:
this->text.setFillColor(this->textActiveColor);
break;
default:
this->shape.setFillColor(sf::Color::Red);
this->text.setFillColor(sf::Color::Blue);
break;
}
}
void Button::render(sf::RenderWindow* target)
{
target->draw(this->shape);
target->draw(this->text);
}
//button.h
#pragma once
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<sstream>
#include"SFML\System.hpp"
#include"SFML\Window.hpp"
#include"SFML\Graphics.hpp"
#include"SFML\Audio.hpp"
enum button_states { BTN_IDLE = 0, BTN_HOVER, BTN_ACTIVE };
class Button
{
private:
short unsigned buttonState;
sf::RectangleShape shape;
sf::Font font;
sf::Text text;
sf::Color textIdleColor;
sf::Color textHoverColor;
sf::Color textActiveColor;
public:
Button(float x, float y, float width, float height,
std::string text, unsigned character_size,
sf::Color text_idle_color, sf::Color text_hover_color, sf::Color text_active_color);
~Button();
//Accessors
const bool isPressed() const;
//Functions
void update(const sf::Vector2f mousePos);
void render(sf::RenderWindow* target);
};
//animation.cpp
#include "Animation.h"
Animation::Animation(std::vector<sf::Texture> textures)
{
this->textures = textures;
}
Animation::Animation(std::vector<sf::Texture> textures, bool loop)
{
this->textures = textures;
this->loop = loop;
}
Animation::Animation(std::vector<sf::Texture> textures, bool loop, float switchTime)
{
this->textures = textures;
this->loop = loop;
this->switchTime = switchTime;
}
void Animation::Reset()
{
currentTexture = 0;
}
sf::Texture* Animation::GetTexture()
{
sf::Texture* textToReturn = &textures[currentTexture];
if (currentTexture == textures.size() - 1)
{
if (loop)
currentTexture = 0;
}
else
currentTexture++;
return textToReturn;
}
// animation.h#pragma once
#include <SFML/Graphics.hpp>
class Animation
{
public:
float switchTime = 0.05f;
Animation(std::vector<sf::Texture> textures);
Animation(std::vector<sf::Texture> textures, bool loop);
Animation(std::vector<sf::Texture> textures, bool loop, float switchTime);
sf::Texture* GetTexture();
void Reset();
private:
bool loop = true;
std::vector<sf::Texture> textures;
int currentTexture = 0;
};
// animator.cpp
#include "Animator.h"
Animator::Animator(sf::RectangleShape* body)
{
this->body = body;
}
void Animator::SetAnimationClip(Animation* anim)
{
totalTime = 0;
if(currentClip != NULL)
currentClip->Reset();
currentClip = anim;
}
void Animator::Update(float deltaTime)
{
totalTime += deltaTime;
if (currentClip != NULL)
{
if (totalTime >= currentClip->switchTime)
{
totalTime -= currentClip->switchTime;
sf::Texture* t = currentClip->GetTexture();
if (t != NULL)
body->setTexture(t);
}
}
}
// animator.h
#pragma once
#include "Animation.h"
#include <SFML/Graphics.hpp>
class Animator
{
public:
Animator(sf::RectangleShape* body);
void SetAnimationClip(Animation* anim);
void Update(float deltaTime);
private:
Animation* currentClip;
sf::RectangleShape* body;
float totalTime;
};
// audioAssets.h
#pragma once
#define AUDIO_GAME_START "Resources/Sound/game_start.wav"
#define AUDIO_EAT_GHOST "Resources/Sound/eat_ghost.wav"
#define AUDIO_DEATH_1 "Resources/Sound/death_1.wav"
#define AUDIO_POWER_SNACK "Resources/Sound/power_pellet.wav"
#define AUDIO_MUNCH "Resources/Sound/munch.wav"
#define AUDIO_SIREN "Resources/Sound/siren_1.wav"
#define AUDIO_RETREATING "Resources/Sound/retreating.wav"
#define VOLUME 50
#define VOLUME_MUNCH 10
#define VOLUME_SIREN 60
enum class Sounds {
None,
PowerSnack,
EatGhost,
Death,
Munch,
GameStart,
Siren,
Retreating
};
// audiomanager.cpp
#include "AudioManager.h"
#include <iostream>
AudioManager::AudioManager()
{
munchSoundBuffer.loadFromFile(AUDIO_MUNCH);
munchSound.setBuffer(munchSoundBuffer);
powerSnackBuffer.loadFromFile(AUDIO_POWER_SNACK);
powerSnackSound.setBuffer(powerSnackBuffer);
eatGhostBuffer.loadFromFile(AUDIO_EAT_GHOST);
eatGhostSound.setBuffer(eatGhostBuffer);
deathBuffer.loadFromFile(AUDIO_DEATH_1);
deathSound.setBuffer(deathBuffer);
gameStartBuffer.loadFromFile(AUDIO_GAME_START);
gameStartSound.setBuffer(gameStartBuffer);
sirenBuffer.loadFromFile(AUDIO_SIREN);
sirenSound.setBuffer(sirenBuffer);
retreatingBuffer.loadFromFile(AUDIO_RETREATING);
retreatingSound.setBuffer(retreatingBuffer);
}
void AudioManager::PlaySound(Sounds soundType, bool loop, int volume)
{
//new way
switch (soundType)
{
case Sounds::PowerSnack:
powerSnackSound.setLoop(loop);
powerSnackSound.setVolume(volume);
powerSnackSound.play();
break;
case Sounds::EatGhost:
eatGhostSound.setLoop(loop);
eatGhostSound.setVolume(volume);
eatGhostSound.play();
break;
case Sounds::Death:
deathSound.setLoop(loop);
deathSound.setVolume(volume);
deathSound.play();
break;
case Sounds::Munch:
munchSound.setLoop(loop);
munchSound.setVolume(volume);
munchSound.play();
break;
case Sounds::GameStart:
gameStartSound.setLoop(loop);
gameStartSound.setVolume(volume);
gameStartSound.play();
break;
case Sounds::Siren:
sirenSound.setLoop(loop);
sirenSound.setVolume(volume);
sirenSound.play();
break;
case Sounds::Retreating:
retreatingSound.setLoop(loop);
retreatingSound.setVolume(volume);
retreatingSound.play();
break;
}
}
void AudioManager::StopSound(Sounds soundType)
{
switch (soundType)
{
case Sounds::PowerSnack:
powerSnackSound.stop();
break;
case Sounds::EatGhost:
eatGhostSound.stop();
break;
case Sounds::Death:
deathSound.stop();
break;
case Sounds::Munch:
munchSound.stop();
break;
case Sounds::GameStart:
gameStartSound.stop();
break;
case Sounds::Siren:
sirenSound.stop();
break;
case Sounds::Retreating:
retreatingSound.stop();
break;
case Sounds::None:
gameStartSound.stop();
munchSound.stop();
deathSound.stop();
eatGhostSound.stop();
powerSnackSound.stop();
sirenSound.stop();
retreatingSound.stop();
break;
}
}
bool AudioManager::IsPlayingAudio(Sounds soundType)
{
switch (soundType)
{
case Sounds::PowerSnack:
return powerSnackSound.getStatus() == powerSnackSound.Playing;
break;
case Sounds::EatGhost:
return eatGhostSound.getStatus() == eatGhostSound.Playing;
break;
case Sounds::Death:
return deathSound.getStatus() == deathSound.Playing;
break;
case Sounds::Munch:
return munchSound.getStatus() == munchSound.Playing;
break;
case Sounds::GameStart:
return gameStartSound.getStatus() == gameStartSound.Playing;
break;
case Sounds::Siren:
return sirenSound.getStatus() == sirenSound.Playing;
break;
case Sounds::Retreating:
return retreatingSound.getStatus() == retreatingSound.Playing;
break;
}
}
// audioManager.h
#pragma once
#include "SFML/Audio.hpp"
#include "AudioAssets.h"
//ATTENTION!! I know there is much better ways to do a audio manager, but i am feeling really lazy and i just want to finish this project as soon as possible, so DONT JUDGE ME...thanks
class AudioManager
{
public:
AudioManager();
void PlaySound(Sounds soundType, bool loop, int volume);
void StopSound(Sounds soundType = Sounds::None);
bool IsPlayingAudio(Sounds soundType);
private:
sf::SoundBuffer gameStartBuffer;
sf::Sound gameStartSound;
sf::SoundBuffer munchSoundBuffer;
sf::Sound munchSound;
sf::SoundBuffer powerSnackBuffer;
sf::Sound powerSnackSound;
sf::SoundBuffer eatGhostBuffer;
sf::Sound eatGhostSound;
sf::SoundBuffer deathBuffer;
sf::Sound deathSound;
sf::SoundBuffer sirenBuffer;
sf::Sound sirenSound;
sf::SoundBuffer retreatingBuffer;
sf::Sound retreatingSound;
};
Editor is loading...