Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
DeC2018 committed May 9, 2023
1 parent 6da276a commit ef0ad0e
Show file tree
Hide file tree
Showing 51 changed files with 2,106 additions and 0 deletions.
90 changes: 90 additions & 0 deletions Bullet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "bullet.h"

// The constructor
Bullet::Bullet()
{
m_BulletShape.setSize(sf::Vector2f(2, 2));
}

void Bullet::shoot(float startX, float startY,
float targetX, float targetY)
{
// Keep track of the bullet
m_InFlight = true;
m_Position.x = startX;
m_Position.y = startY;

// Calculate the gradient of the flight path
float gradient = (startX - targetX) / (startY - targetY);

// Any gradient less than 1 needs to be negative
if (gradient < 0)
{
gradient *= -1;
}

// Calculate the ratio between x and y
float ratioXY = m_BulletSpeed / (1 + gradient);

// Set the "speed" horizontally and vertically
m_BulletDistanceY = ratioXY;
m_BulletDistanceX = ratioXY * gradient;

// Point the bullet in the right direction
if (targetX < startX)
{
m_BulletDistanceX *= -1;
}

if (targetY < startY)
{
m_BulletDistanceY *= -1;
}

// Set a max range of 1000 pixels
float range = 1000;
m_MinX = startX - range;
m_MaxX = startX + range;
m_MinY = startY - range;
m_MaxY = startY + range;

// Position the bullet ready to be drawn
m_BulletShape.setPosition(m_Position);
}

void Bullet::stop()
{
m_InFlight = false;
}

bool Bullet::isInFlight()
{
return m_InFlight;
}

FloatRect Bullet::getPosition()
{
return m_BulletShape.getGlobalBounds();
}

RectangleShape Bullet::getShape()
{
return m_BulletShape;
}

void Bullet::update(float elapsedTime)
{
// Update the bullet position variables
m_Position.x += m_BulletDistanceX * elapsedTime;
m_Position.y += m_BulletDistanceY * elapsedTime;

// Move the bullet
m_BulletShape.setPosition(m_Position);

// Has the bullet gone out of range?
if (m_Position.x < m_MinX || m_Position.x > m_MaxX ||
m_Position.y < m_MinY || m_Position.y > m_MaxY)
{
m_InFlight = false;
}
}
57 changes: 57 additions & 0 deletions Bullet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once
#include <SFML/Graphics.hpp>

using namespace sf;

class Bullet
{
private:
// Where is the bullet?
Vector2f m_Position;

// What each bullet looks like
RectangleShape m_BulletShape;

// Is this bullet currently whizzing through the air
bool m_InFlight = false;

// How fast does a bullet travel?
float m_BulletSpeed = 1000;

// What fraction of 1 pixel does the bullet travel,
// Horizontally and vertically each frame?
// These values will be derived from m_BulletSpeed
float m_BulletDistanceX;
float m_BulletDistanceY;

// Some boundaries so the bullet doesn't fly forever
float m_MaxX;
float m_MinX;
float m_MaxY;
float m_MinY;

// Public function prototypes go here
public:
// The constructor
Bullet();

// Stop the bullet
void stop();

// Returns the value of m_InFlight
bool isInFlight();

// Launch a new bullet
void shoot(float startX, float startY,
float xTarget, float yTarget);

// Tell the calling code where the bullet is in the world
FloatRect getPosition();

// Return the actual shape (for drawing)
RectangleShape getShape();

// Update the bullet each frame
void update(float elapsedTime);
};

65 changes: 65 additions & 0 deletions CreateBackground.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <SFML/Graphics.hpp>
#include "ZombieArena.h"

int createBackground(VertexArray& rVA, IntRect arena)
{
// Anything we do to rVA we are actually doing to background (in the main function)

// How big is each tile/texture
const int TILE_SIZE = 50;
const int TILE_TYPES = 3;
const int VERTS_IN_QUAD = 4;

int worldWidth = arena.width / TILE_SIZE;
int worldHeight = arena.height / TILE_SIZE;

// What type of primitive are we using?
rVA.setPrimitiveType(Quads);

// Set the size of the vertex array
rVA.resize(worldWidth * worldHeight * VERTS_IN_QUAD);

// Start at the beginning of the vertex array
int currentVertex = 0;

for (int w = 0; w < worldWidth; w++)
{
for (int h = 0; h < worldHeight; h++)
{
// Position each vertex in the current quad
rVA[currentVertex + 0].position = Vector2f(w * TILE_SIZE, h * TILE_SIZE);
rVA[currentVertex + 1].position = Vector2f((w * TILE_SIZE) + TILE_SIZE, h * TILE_SIZE);
rVA[currentVertex + 2].position = Vector2f((w * TILE_SIZE) + TILE_SIZE, (h * TILE_SIZE) + TILE_SIZE);
rVA[currentVertex + 3].position = Vector2f((w * TILE_SIZE), (h * TILE_SIZE) + TILE_SIZE);

// Define the position in the Texture to draw for current quad
// Either mud, stone, grass or wall
if (h == 0 || h == worldHeight - 1 || w == 0 || w == worldWidth - 1)
{
// Use the wall texture
rVA[currentVertex + 0].texCoords = Vector2f(0, 0 + TILE_TYPES * TILE_SIZE);
rVA[currentVertex + 1].texCoords = Vector2f(TILE_SIZE, 0 + TILE_TYPES * TILE_SIZE);
rVA[currentVertex + 2].texCoords = Vector2f(TILE_SIZE, TILE_SIZE + TILE_TYPES * TILE_SIZE);
rVA[currentVertex + 3].texCoords = Vector2f(0, TILE_SIZE + TILE_TYPES * TILE_SIZE);
}
else
{
// Use a random floor texture
srand((int)time(0) + h * w - h);
int mOrG = (rand() % TILE_TYPES);
int verticalOffset = mOrG * TILE_SIZE;

rVA[currentVertex + 0].texCoords = Vector2f(0, 0 + verticalOffset);
rVA[currentVertex + 1].texCoords = Vector2f(TILE_SIZE, 0 + verticalOffset);
rVA[currentVertex + 2].texCoords = Vector2f(TILE_SIZE, TILE_SIZE + verticalOffset);
rVA[currentVertex + 3].texCoords = Vector2f(0, TILE_SIZE + verticalOffset);

}

// Position ready for the next for vertices
currentVertex = currentVertex + VERTS_IN_QUAD;
}
}

return TILE_SIZE;
}
59 changes: 59 additions & 0 deletions CreateHorde.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "ZombieArena.h"
#include "Zombie.h"

Zombie* createHorde(int numZombies, IntRect arena)
{
Zombie* zombies = new Zombie[numZombies];

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 < numZombies; i++)
{

// Which side should the zombie 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;
}

// Bloater, crawler or runner
srand((int)time(0) * i * 2);
int type = (rand() % 3);

// Spawn the new zombie into the array
zombies[i].spawn(x, y, type, i);

}
return zombies;
}


Loading

0 comments on commit ef0ad0e

Please sign in to comment.