Skip to content

Commit

Permalink
axis-aligned hitboxes, hitcircles, other minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wolearyc committed May 3, 2017
1 parent 4e8170e commit b9e7c31
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 16 deletions.
79 changes: 79 additions & 0 deletions include/Hitbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* ----------------------------------------------------------------------------
* wic - a simple 2D game engine for MacOS written in C++
* Copyright (C) 2013-2017 Willis O'Leary
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
* ----------------------------------------------------------------------------
* File: Hitbox.h
* ----------------------------------------------------------------------------
*/
/** \file */
#ifndef HITBOX_H
#define HITBOX_H
#include "Interfaces.h"
namespace wic
{
class Contact
{
public:
Contact(Pair point, double depth, Pair normal);
Contact();
bool isIntersecting() const;
Pair getPoint() const;
double getDepth() const;
Pair getNormal() const;
private:
bool intersecting;
Pair point;
double depth;
Pair normal;
};
class HitBoxAligned;
class HitCircle;

class HitBoxAligned
: public Locateable, public Scaleable
{
public:
HitBoxAligned(Pair location, Pair dimensions);
HitBoxAligned();
HitBoxAligned(const HitBoxAligned& other);
bool isIntersecting(const HitBoxAligned& other) const;
bool isIntersecting(const HitCircle& other) const;
Contact getContact(const HitBoxAligned& other) const;
Contact getContact(const HitCircle& other) const;
Pair getGeoCenter() const;
Pair getMin() const;
Pair getMax() const;
bool centered;
Pair dimensions;
};

class HitCircle
: public Locateable, public Scaleable
{
public:
HitCircle(Pair location, double radius);
HitCircle();
HitCircle(const HitCircle& other);
bool isIntersecting(const HitBoxAligned& other) const;
bool isIntersecting(const HitCircle& other) const;
Contact getContact(const HitBoxAligned& other) const;
Contact getContact(const HitCircle& other) const;
Pair getGeoCenter() const;
bool centered;
double radius;
};
}
#endif
15 changes: 15 additions & 0 deletions include/Pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,27 @@ namespace wic
* \return the norm of the Pair
*/
double norm() const;
/** Returns a normalized Pair. */
Pair normalized() const;
/** Returns a Pair with negative components made positive. */
Pair abs() const;
/** Returns the square of the norm. */
double normSquared() const;
/** Returns the dot product with another pair
* \param other another Pair
*/
double dot(const Pair& other) const;
/** Transforms the pair based on a rotation, scale, and center.
* \param rotation the rotation
* \param scale the scale
* \param center the center to rotate and scale around
*/
void transform(double rotation, Pair scale, Pair center);
/** Transforms the pair based on a scale and center.
* \param scale the scale
* \param center the center to rotate and scale around
*/
void transform(Pair scale, Pair center);
/** Defines unary negation.
* \return the component-wise negation of the pair
*/
Expand Down
22 changes: 18 additions & 4 deletions include/Stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#ifndef STAGE_H
#define STAGE_H
#include <vector>
#include <initializer_list>
#include "Interfaces.h"
#include "Camera.h"
#include "Actor.h"
Expand All @@ -35,11 +36,11 @@ namespace wic
/** Updates actors. This includes handing actions of singular actors and
* interactions between actors.
*/
virtual void updtActors() = 0;
virtual void updt() = 0;
/** Draws actors.
* \param camera the camera through which to view
*/
virtual void drawActors(const Camera& camera) = 0;
virtual void draw(const Camera& camera) = 0;
};

/** Indicates that a Stage can contain actors of a certain type.
Expand All @@ -61,7 +62,7 @@ namespace wic
/** Updates actors and removes those marked for removal. This method
* handles individual actions.
*/
void updtAll()
void handle()
{
for(auto actor = actors.begin() ; actor != actors.end(); ++actor)
actor->act();
Expand All @@ -73,6 +74,19 @@ namespace wic
protected:
vector<ActorClass> actors; /**< List of actors */
};

template <class ActorClass, class ActingOn> class ContainsInteracting
{
public:
void handle(vector<ActorClass> actors, vector<ActingOn> others)
{
for(auto actor = actors.begin(); actor != actors.end(); ++actor)
{
for(auto other = others.begin(); other != others.end(); ++other)
{
actor->actOn(*other);
}
}
}
};
}
#endif
2 changes: 1 addition & 1 deletion src/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace wic
const Color Color::Navy = Color(0,0,128);
const Color Color::Fuchsia = Color(255,0,255);
const Color Color::Purple = Color(128,0,128);
const Color Color::WicOrange = Color(253,144,11,255);
const Color Color::WicOrange = Color(244,126,23,255);
const Color Color::WicGray = Color(63,63,63,255);
Color::Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
: red(red), green(green), blue(blue), alpha(alpha)
Expand Down
Loading

0 comments on commit b9e7c31

Please sign in to comment.