Skip to content

Commit

Permalink
Merge pull request #6 from claudiosegala/sixth
Browse files Browse the repository at this point in the history
Sixth
  • Loading branch information
claudiosegala authored May 12, 2019
2 parents 2d81917 + 01cbf32 commit e7a388c
Show file tree
Hide file tree
Showing 45 changed files with 1,288 additions and 633 deletions.
58 changes: 35 additions & 23 deletions include/Alien.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <Component.h>
#include <GameObject.h>
#include <Logger.h>
#include <Timer.h>
#include <Vec2.h>
#include <Util.h>

#include <string>
Expand All @@ -13,46 +15,56 @@
class Alien : public Component {
public:

Alien(GameObject&, int);
static int alienCount;

~Alien();
Alien(GameObject&, int);

void Start();
~Alien();

void Update(float);
void Start();

void Render();
void Update(float);

bool Is(std::string);
void Render();

void NotifyCollision(GameObject&);

bool Is(std::string);

private:

class Action {
public:

enum class ActionType {
MOVE,
SHOOT
static int const life;

static int const restCoolDown;

static float const pace;

static float const spinPace;

enum class AlienState {
MOVING,
RESTING,
NOP
};

Vec2 pos;

ActionType type;
int hp;

Action (ActionType, float, float);
Vec2 speed;

};
Vec2 destination;

Timer restTimer;

void Move(Action, float);
AlienState state;

void Shoot(Action);
std::vector<std::weak_ptr<GameObject>> minions;

Vec2 speed;
void Rest(float);

int hp;
void Move();

std::queue<Action> taskQueue;
void Shoot(Vec2);

std::vector<std::weak_ptr<GameObject>> minions;
int GetClosestMinion(Vec2);

};
29 changes: 20 additions & 9 deletions include/Bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,33 @@

class Bullet : public Component {
public:

Bullet(GameObject&, float, float, int, float, std::string);

void Update(float);
static float const defaultSpeed;

void Render();
static float const defaultDamage;

bool Is(std::string);
static float const defaultMaxDistance;

int GetDamage();
bool targetPlayer;

Bullet(GameObject&, float, float, int, float, std::string, int frameCount = 1, float frameTime = 1.0f, bool targetPlayer = false);

void Update(float);

void Render();

void NotifyCollision(GameObject&);

bool Is(std::string);

int GetDamage();

private:

Vec2 speed;
int damage;

float distanceLeft;
float distanceLeft;

int damage;
Vec2 speed;

};
16 changes: 9 additions & 7 deletions include/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
class Camera {
public:

static Vec2 pos;
static Vec2 pos;

static Vec2 speed;
static Vec2 speed;

static void Follow(GameObject*);
static void Follow(GameObject*);

static void Unfollow();
static void Unfollow();

static void Update(float);
static void Update(float);

private:

static GameObject* focus;
static float const pace;

static Vec2 GetMovement();
static GameObject* focus;

static Vec2 GetMovement();
};
10 changes: 5 additions & 5 deletions include/CameraFollower.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
class CameraFollower : public Component {
public:

CameraFollower (GameObject&);
CameraFollower (GameObject&);

void Update(float);
void Update(float);

void Render();

bool Is(std::string);
void Render();
bool Is(std::string);
};
33 changes: 33 additions & 0 deletions include/Collider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <Util.h>
#include <GameObject.h>
#include <Collision.h>
#include <Component.h>
#include <Vec2.h>
#include <Rect.h>

class Collider : public Component {
public:

Rect box;

Collider (GameObject&, Vec2 scale = {1, 1}, Vec2 offset = {0, 0});

void Update(float);

void Render();

bool Is(std::string);

void SetScale(Vec2);

void SetOffset(Vec2);

private:

Vec2 offset;

Vec2 scale;

};
91 changes: 91 additions & 0 deletions include/Collision.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#pragma once

#include <Rect.h>
#include <Vec2.h>

#include <algorithm>
#include <cmath>

class Collision {
public:

// Observação: IsColliding espera ângulos em radianos!
// Para usar graus, forneça a sua própria implementação de Rotate,
// ou transforme os ângulos no corpo de IsColliding.
static inline bool IsColliding(Rect& a, Rect& b, float angleOfA, float angleOfB) {
Vec2 A[] = { Vec2( a.vector.x, a.vector.y + a.height ),
Vec2( a.vector.x + a.width, a.vector.y + a.height ),
Vec2( a.vector.x + a.width, a.vector.y ),
Vec2( a.vector.x, a.vector.y )
};
Vec2 B[] = { Vec2( b.vector.x, b.vector.y + b.height ),
Vec2( b.vector.x + b.width, b.vector.y + b.height ),
Vec2( b.vector.x + b.width, b.vector.y ),
Vec2( b.vector.x, b.vector.y )
};

for (auto& v : A) {
v = Rotate(v - a.Center(), angleOfA) + a.Center();
}

for (auto& v : B) {
v = Rotate(v - b.Center(), angleOfB) + b.Center();
}

Vec2 axes[] = { Norm(A[0] - A[1]), Norm(A[1] - A[2]), Norm(B[0] - B[1]), Norm(B[1] - B[2]) };

for (auto& axis : axes) {
float P[4];

for (int i = 0; i < 4; ++i) P[i] = Dot(A[i], axis);

float minA = *std::min_element(P, P + 4);
float maxA = *std::max_element(P, P + 4);

for (int i = 0; i < 4; ++i) P[i] = Dot(B[i], axis);

float minB = *std::min_element(P, P + 4);
float maxB = *std::max_element(P, P + 4);

if (maxA < minB || minA > maxB)
return false;
}

return true;
}

private:

static inline float Mag(const Vec2& p) {
return std::sqrt(p.x * p.x + p.y * p.y);
}

static inline Vec2 Norm(const Vec2& p) {
return p * (1.f / Mag(p));
}

static inline float Dot(const Vec2& a, const Vec2& b) {
return a.x * b.x + a.y * b.y;
}

static inline Vec2 Rotate(const Vec2& p, float angle) {
float cs = std::cos(angle), sn = std::sin(angle);
return Vec2 ( p.x * cs - p.y * sn, p.x * sn + p.y * cs );
}
};

// Aqui estão três operadores que sua classe Vec2 deve precisar, se já não tiver.
// Se sua classe tiver métodos para Mag, Norm, Dot e/ou Rotate, você pode substituir
// os usos desses métodos por usos dos seus, mas garanta que deem resultados corretos.

// Vec2 operator+(const Vec2& rhs) const {
// return Vec2(x + rhs.x, y + rhs.y);
// }

// Vec2 operator-(const Vec2& rhs) const {
// return Vec2(x - rhs.x, y - rhs.y);
// }

// Vec2 operator*(const float rhs) const {
// return Vec2(x * rhs, y * rhs);
// }
16 changes: 9 additions & 7 deletions include/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ class GameObject;
class Component {
public:

Component(GameObject&);
Component(GameObject&);

virtual ~Component();
virtual ~Component();

virtual void Start();
virtual void Start();

virtual void Update(float dt) = 0;
virtual void NotifyCollision(GameObject&);

virtual void Render() = 0;
virtual void Update(float dt) = 0;

virtual bool Is(std::string) = 0;
virtual void Render() = 0;

virtual bool Is(std::string) = 0;

protected:

GameObject& associated;
GameObject& associated;

};
Loading

0 comments on commit e7a388c

Please sign in to comment.