Commit 8675ec7e authored by alaskalinuxuser's avatar alaskalinuxuser
Browse files

Section 8 complete: A JelloStorm is underway!

parent f6c5d422
#include <sstream>
#include <Jello.h>
#include <JelloStorm.h>
Jello* createHorde(int numJellos, IntRect arena)
{
Jello* jellos = new Jello[numJellos];
int maxY = arena.height - 20;
int minY = arena.top + 20;
int maxX = arena.width - 20;
int minX = arena.left + 20;
for (int i = 0; i < numJellos; i++)
{
// Which side should the jello spawn
srand((int)time(0) * i);
int side = (rand() % 4);
float x, y;
switch (side)
{
case 0:
// left
x = minX;
y = (rand() % maxY) + minY;
break;
case 1:
// right
x = maxX;
y = (rand() % maxY) + minY;
break;
case 2:
// top
x = (rand() % maxX) + minX;
y = minY;
break;
case 3:
// bottom
x = (rand() % maxX) + minX;
y = maxY;
break;
}
// Red, Blue, or Green
srand((int)time(0) * i * 2);
int type = (rand() % 3);
// Spawn the new jello into the array
jellos[i].spawn(x, y, type, i);
}
return jellos;
}
#include <sstream>
#include <Jello.h>
#include <TextureHolder.h>
#include <cstdlib>
#include <ctime>
using namespace std;
void Jello::spawn(float startX, float startY, int type, int seed)
{
switch (type)
{
case 0:
// Red Jello
m_Sprite = Sprite(TextureHolder::GetTexture(
"graphics/red_jello.png"));
m_Speed = 40;
m_Health = 5;
break;
case 1:
// Blue Jello
m_Sprite = Sprite(TextureHolder::GetTexture(
"graphics/blue_jello.png"));
m_Speed = 70;
m_Health = 1;
break;
case 2:
// Green Jello
m_Sprite = Sprite(TextureHolder::GetTexture(
"graphics/green_jello.png"));
m_Speed = 20;
m_Health = 3;
break;
}
// Modify the speed to make the Jello unique
// Every jello is unique. Create a speed modifier
srand((int)time(0) * seed);
// Somewhere between 80 an 100
float modifier = (rand() % MAX_VARRIANCE) + OFFSET;
// Express as a fraction of 1
modifier /= 100; // Now equals between .7 and 1
m_Speed *= modifier;
m_Position.x = startX;
m_Position.y = startY;
m_Sprite.setOrigin(25, 25);
m_Sprite.setPosition(m_Position);
}
bool Jello::hit()
{
m_Health--;
if (m_Health < 0)
{
// dead
m_Alive = false;
m_Sprite.setTexture(TextureHolder::GetTexture(
"graphics/smearedjello.png"));
return true;
}
// injured but not dead yet
return false;
}
bool Jello::isAlive()
{
return m_Alive;
}
FloatRect Jello::getPosition()
{
return m_Sprite.getGlobalBounds();
}
Sprite Jello::getSprite()
{
return m_Sprite;
}
void Jello::update(float elapsedTime,
Vector2f playerLocation)
{
float playerX = playerLocation.x;
float playerY = playerLocation.y;
// Update the jello position variables
if (playerX > m_Position.x)
{
m_Position.x = m_Position.x +
m_Speed * elapsedTime;
}
if (playerY > m_Position.y)
{
m_Position.y = m_Position.y +
m_Speed * elapsedTime;
}
if (playerX < m_Position.x)
{
m_Position.x = m_Position.x -
m_Speed * elapsedTime;
}
if (playerY < m_Position.y)
{
m_Position.y = m_Position.y -
m_Speed * elapsedTime;
}
// Move the sprite
m_Sprite.setPosition(m_Position);
// Face the sprite in the correct direction
float angle = (atan2(playerY - m_Position.y,
playerX - m_Position.x)
* 180) / 3.141;
m_Sprite.setRotation(angle);
}
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;
class Jello {
private:
// How fast is each jello type?
const float RED_SPEED = 40; //Bloater in original game.
const float BLUE_SPEED = 80; //Chaser in original game.
const float GREEN_SPEED = 20; //Crawler in original game.
// How tough is each jello type
const float RED_HEALTH = 5;
const float BLUE_HEALTH = 1;
const float GREEN_HEALTH = 3;
// Make each jello vary its speed slightly
const int MAX_VARRIANCE = 30;
const int OFFSET = 101 - MAX_VARRIANCE;
// Where is this jello?
Vector2f m_Position;
// A sprite for the jello
Sprite m_Sprite;
// How fast can this one run/crawl?
float m_Speed;
// How much health has it got?
float m_Health;
// Is it still alive?
bool m_Alive;
// Public prototypes go here
public:
// Handle when a bullet hits a jello
bool hit();
// Find out if the jello is alive
bool isAlive();
// Spawn a new jello
void spawn(float startX, float startY, int type, int seed);
// Return a rectangle that is the position in the world
FloatRect getPosition();
// Get a copy of the sprite to draw
Sprite getSprite();
// Update the jello each frame
void update(float elapsedTime, Vector2f playerLocation);
};
......@@ -3,6 +3,7 @@
#include <SFML/Audio.hpp>
#include <Player.h>
#include <JelloStorm.h>
#include <TextureHolder.h>
// Declare the functions....
......@@ -11,6 +12,8 @@ using namespace sf;
// The main process....
int main() {
TextureHolder holder;
// What is the game state?
enum class State {
PAUSED, LEVELING_UP, GAME_OVER, PLAYING
......@@ -53,8 +56,12 @@ int main() {
// Create our background tiles
VertexArray tileBackground;
Texture textureBackground;
textureBackground.loadFromFile("graphics/background_sheet.png");
Texture textureBackground = TextureHolder::GetTexture("graphics/background_sheet.png");
// Time to make jello!
int numJellos;
int numJellosAlive;
Jello* jellos = nullptr;
while (window.isOpen()) {
......@@ -201,6 +208,14 @@ int main() {
// Spawn the player in the middle of the arena
player.spawn(arena, resolution, tileSize);
// Make some Jello!
numJellos = 10;
// Delete the previously allocated memory (if it exists)
delete[] jellos;
jellos = createHorde(numJellos, arena);
numJellosAlive = numJellos;
// Reset the clock so there isn't a frame jump
clock.restart();
}
......@@ -237,6 +252,16 @@ int main() {
// Make the view centre around the player
mainView.setCenter(player.getCenter());
// Loop through each Jello and update them
for (int i = 0; i < numJellos; i++)
{
if (jellos[i].isAlive())
{
jellos[i].update(dt.asSeconds(), playerPosition);
}
}
}// End updating the scene
......@@ -256,6 +281,12 @@ int main() {
window.setView(mainView);
window.draw(tileBackground, &textureBackground);
// Draw the Jello!
for (int i = 0; i < numJellos; i++)
{
window.draw(jellos[i].getSprite());
}
// Draw the player
window.draw(player.getSprite());
......@@ -263,22 +294,25 @@ int main() {
if (state == State::LEVELING_UP)
{
window.clear();
//window.clear();
}
if (state == State::PAUSED)
{
window.clear();
//window.clear();
}
if (state == State::GAME_OVER)
{
window.clear();
//window.clear();
}
window.display();
}
// Delete the previously allocated memory (if it exists)
delete[] jellos; // Clean up is always a good idea.
return 0;
}
#pragma once
#include <Jello.h>
using namespace sf;
int createBackground(VertexArray& rVA, IntRect arena);
Jello* createHorde(int numJellos, IntRect arena);
No preview for this file type
#include <sstream>
#include <Player.h>
#include <TextureHolder.h>
using namespace sf;
using namespace sf;
......@@ -13,8 +14,8 @@ Player::Player()
m_MaxHealth = START_HEALTH;
// Give the player graphics.
m_Texture.loadFromFile("graphics/archer.png");
m_Sprite.setTexture(m_Texture);
m_Sprite = Sprite(TextureHolder::GetTexture(
"graphics/archer.png"));
// Set the center of the sprite so it can rotate.
m_Sprite.setOrigin(25, 25);
......
No preview for this file type
#include <sstream>
#include <TextureHolder.h>
#include <assert.h>
using namespace sf;
using namespace std;
TextureHolder* TextureHolder::m_s_Instance = nullptr;
TextureHolder::TextureHolder()
{
assert(m_s_Instance == nullptr);
m_s_Instance = this;
}
sf::Texture& TextureHolder::GetTexture(std::string const& filename)
{
// Get a reference to m_Textures using m_S_Instance
auto& m = m_s_Instance->m_Textures;
// auto is the equivalent of map<string, Texture>
// Create an iterator to hold a key-value-pair (kvp)
// and search for the required kvp
// using the passed in file name
auto keyValuePair = m.find(filename);
// auto is equivelant of map<string, Texture>::iterator
// Did we find a match?
if (keyValuePair != m.end())
{
// Yes
// Return the texture,
// the second part of the kvp, the texture
return keyValuePair->second;
}
else
{
// File name not found
// Create a new key value pair using the filename
auto& texture = m[filename];
// Load the texture from file in the usual way
texture.loadFromFile(filename);
// Return the texture to the calling code
return texture;
}
}
#ifndef TEXTURE_HOLDER_H
#define TEXTURE_HOLDER_H
#include <SFML/Graphics.hpp>
#include <map>
using namespace sf;
class TextureHolder {
private:
// A map container from the STL,
// that holds related pairs of String and Texture
std::map<std::string, sf::Texture> m_Textures;
// A pointer of the same type as the class itself
// the one and only instance
static TextureHolder* m_s_Instance;
public:
TextureHolder();
static Texture& GetTexture(std::string const& filename);
};
#endif
......@@ -2,11 +2,14 @@
# You need libsfml installed on your system!
g++ -I./ -Wall -Wextra -g -c -std=c++11 TextureHolder.cpp -o TextureHolder.o
g++ -I./ -Wall -Wextra -g -c -std=c++11 Player.cpp -o Player.o
g++ -I./ -Wall -Wextra -g -c -std=c++11 Jello.cpp -o Jello.o
g++ -I./ -Wall -Wextra -g -c -std=c++11 CreateHorde.cpp -o CreateHorde.o
g++ -I./ -Wall -Wextra -g -c -std=c++11 CreateBackground.cpp -o CreateBackground.o
g++ -I./ -Wall -Wextra -g -c -std=c++11 JelloStorm.cpp -o JelloStorm.o
g++ JelloStorm.o Player.o CreateBackground.o -o JelloStorm-app -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
g++ JelloStorm.o Player.o Jello.o CreateHorde.o CreateBackground.o TextureHolder.o -o JelloStorm-app -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
./JelloStorm-app
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment