Commit 897162e6 authored by alaskalinuxuser's avatar alaskalinuxuser
Browse files

Section 9, part 1, Added arrows, and updated to c++ standard for 2017.

parent 8675ec7e
#include <sstream>
#include <Arrow.h>
#include <cmath>
// The constructor
Arrow::Arrow()
{
m_ArrowShape.setSize(sf::Vector2f(2, 2));
}
void Arrow::shoot(float startX, float startY,
float targetX, float targetY)
{
// Keep track of the arrow
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 zero needs to be negative
if (gradient < 0)
{
gradient *= -1;
}
// Calculate the ratio between x and t
float ratioXY = m_ArrowSpeed / (1 + gradient);
// Set the "speed" horizontally and vertically
m_ArrowDistanceY = ratioXY;
m_ArrowDistanceX = ratioXY * gradient;
// Point the arrow in the right direction
if (targetX < startX)
{
m_ArrowDistanceX *= -1;
}
if (targetY < startY)
{
m_ArrowDistanceY *= -1;
}
// Finally, assign the results to the
// member variables
//m_XTarget = targetX;
//m_YTarget = targetY;
// 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 arrow ready to be drawn
m_ArrowShape.setPosition(m_Position);
}
void Arrow::stop()
{
m_InFlight = false;
}
bool Arrow::isInFlight()
{
return m_InFlight;
}
FloatRect Arrow::getPosition()
{
return m_ArrowShape.getGlobalBounds();
}
RectangleShape Arrow::getShape()
{
return m_ArrowShape;
}
void Arrow::update(float elapsedTime)
{
// Update the arrow position variables
m_Position.x += m_ArrowDistanceX * elapsedTime;
m_Position.y += m_ArrowDistanceY * elapsedTime;
// Move the arrow
m_ArrowShape.setPosition(m_Position);
// Has the arrow 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;
}
}
#pragma once
#include <SFML/Graphics.hpp>
#include <cmath>
using namespace sf;
class Arrow
{
private:
// Where is the arrow?
Vector2f m_Position;
// What each arrow looks like
RectangleShape m_ArrowShape;
//m_ArrowShape.setFillColor(Color(100, 250, 50));
// Is this arrow currently whizzing through the air
bool m_InFlight = false;
// How fast does a arrow travel?
float m_ArrowSpeed = 750;
// What fraction of 1 pixel does the arrow travel,
// Horizontally and vertically each frame?
// These values will be derived from m_ArrowSpeed
float m_ArrowDistanceX;
float m_ArrowDistanceY;
// Where is this arrow headed to?
float m_XTarget;
float m_YTarget;
// Some boundaries so the arrow doesn't fly forever
float m_MaxX;
float m_MinX;
float m_MaxY;
float m_MinY;
// Public function prototypes go here
public:
// The constructor
Arrow();
// Stop the arrow
void stop();
// Returns the value of m_InFlight
bool isInFlight();
// Launch a new arrow
void shoot(float startX, float startY,
float xTarget, float yTarget);
// Tell the calling code where the arrow is in the world
FloatRect getPosition();
// Return the actual shape (for drawing)
RectangleShape getShape();
// Update the arrow each frame
void update(float elapsedTime);
};
No preview for this file type
......@@ -3,6 +3,7 @@
#include <TextureHolder.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
......
#pragma once
#include <SFML/Graphics.hpp>
#include <cmath>
using namespace sf;
......
No preview for this file type
......@@ -4,6 +4,8 @@
#include <Player.h>
#include <JelloStorm.h>
#include <TextureHolder.h>
#include <Arrow.h>
#include <cmath>
// Declare the functions....
......@@ -62,6 +64,16 @@ int main() {
int numJellos;
int numJellosAlive;
Jello* jellos = nullptr;
// 100 Arrows should do
Arrow arrows[100];
int currentArrow = 0;
int arrowsSpare = 24;
int arrowsInQuiver = 6;
int quiverSize = 6;
float fireRate = 1;
// When was the fire button last pressed?
Time lastPressed;
while (window.isOpen()) {
......@@ -107,7 +119,27 @@ int main() {
if (state == State::PLAYING)
{
// Reloading
if (event.key.code == Keyboard::R)
{
if (arrowsSpare >= quiverSize)
{
// Plenty of bullets. Reload.
arrowsInQuiver = quiverSize;
arrowsSpare -= quiverSize;
}
else if (arrowsSpare > 0)
{
// Only few bullets left
arrowsInQuiver = arrowsSpare;
arrowsSpare = 0;
}
else
{
// play a sound...
}
}
}
}
}// End event polling
......@@ -157,6 +189,34 @@ int main() {
{
player.stopRight();
}
// Fire an arrow
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
if (gameTimeTotal.asMilliseconds()
- lastPressed.asMilliseconds()
> 1000 / fireRate && arrowsInQuiver > 0)
{
// Pass the centre of the player
// and the centre of the cross-hair
// to the shoot function
arrows[currentArrow].shoot(
player.getCenter().x, player.getCenter().y,
mouseWorldPosition.x, mouseWorldPosition.y);
currentArrow++;
if (currentArrow > 99)
{
currentArrow = 0;
}
lastPressed = gameTimeTotal;
arrowsInQuiver--;
}
}// End fire an arrow.
}// End WASD while playing
......@@ -262,6 +322,15 @@ int main() {
}
}
// Loop through any arrows that are in flight.
for (int i = 0; i < 100; i++)
{
if (arrows[i].isInFlight())
{
arrows[i].update(dtAsSeconds);
}
}
}// End updating the scene
......@@ -287,6 +356,15 @@ int main() {
{
window.draw(jellos[i].getSprite());
}
// Draw the arrows.
for (int i = 0; i < 100; i++)
{
if (arrows[i].isInFlight())
{
window.draw(arrows[i].getShape());
}
}
// Draw the player
window.draw(player.getSprite());
......
No preview for this file type
#include <sstream>
#include <Player.h>
#include <TextureHolder.h>
using namespace sf;
#include <cmath>
using namespace sf;
......
#pragma once
#include <SFML/Graphics.hpp>
#include <cmath>
using namespace sf;
......
No preview for this file type
# JelloStorm
![ScreenShot](https://github.com/alaskalinuxuser/Cpp_training_material/blob/master/JelloStorm/graphics/sample.png)
# Gameplay
You are an archer who was traveling through a grassy plain. Suddenly, a horde of ferocious jello monsters storm you! Eat grapes to preserve your strength! Find more arrows to shoot at your enemy!
wasd - move the archer.
r - reload the quiver with arrows.
ESC - exit the game.
ENTER - Pause/resume.
Mouse Right click - shoot an arrow.
Number - Input durring level up.
# Background info
A directory of my work while participating in a class about learning how to program in C++.
All of my work is licensed under the Apache2.0, however, some of the class artwork and sounds may not be opensourced.
To build these games, you need libsfml installed on your Linux system. Then, in each game folder, run the ./build.sh file to build the source.
#A note about the build.sh script
std=c++1z is for the 2017 standard of C++.
This will compile under std=c++14 (2014) or std=c++11 (2011) standards as well.
If your compiler doesn't recognize the std=c++1z, you can try one of the above instead.
If your compiler is newer, you could try std=c++17 as well.
My compiler version:
alaskalinuxuser@alaskalinuxuser-OptiPlex-7010:~$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#Installation
Pre-reqs: libsfml
If you are reading this, then you have downloaded and extraxted the package already. All you must do now is:
$ ./build.sh
......@@ -2,14 +2,28 @@
# 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 Jello.o CreateHorde.o CreateBackground.o TextureHolder.o -o JelloStorm-app -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
# std=c++1z is for the 2017 standard of C++.
# This will compile under std=c++14 (2014) or std=c++11 (2011) standards as well.
# If your compiler doesn't recognize the std=c++1z, you can try one of the above instead.
# If your compiler is newer, you could try std=c++17 as well.
# My compiler version:
# alaskalinuxuser@alaskalinuxuser-OptiPlex-7010:~$ g++ --version
# g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
# Copyright (C) 2015 Free Software Foundation, Inc.
# This is free software; see the source for copying conditions. There is NO
# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++ -I./ -Wall -Wextra -g -c -std=c++1z TextureHolder.cpp -o TextureHolder.o
g++ -I./ -Wall -Wextra -g -c -std=c++1z Player.cpp -o Player.o
g++ -I./ -Wall -Wextra -g -c -std=c++1z Arrow.cpp -o Arrow.o
g++ -I./ -Wall -Wextra -g -c -std=c++1z Jello.cpp -o Jello.o
g++ -I./ -Wall -Wextra -g -c -std=c++1z CreateHorde.cpp -o CreateHorde.o
g++ -I./ -Wall -Wextra -g -c -std=c++1z CreateBackground.cpp -o CreateBackground.o
g++ -I./ -Wall -Wextra -g -c -std=c++1z JelloStorm.cpp -o JelloStorm.o
g++ JelloStorm.o Player.o Jello.o CreateHorde.o CreateBackground.o TextureHolder.o Arrow.o -o JelloStorm-app -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
./JelloStorm-app
......
Markdown is supported
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