@@ -0,0 +1,80 @@ | |||
#pragma once | |||
#include <SFML/System/Vector2.hpp> | |||
namespace Swan { | |||
template<typename T> | |||
class Vector2 { | |||
public: | |||
T x_; | |||
T y_; | |||
constexpr Vector2(T x = 0, T y = 0): x_(x), y_(y) {} | |||
operator sf::Vector2<T>() const { | |||
return sf::Vector2<T>(x_, y_); | |||
} | |||
Vector2<T> operator-() const { | |||
return Vector2<T>(-x_, -y_); | |||
} | |||
Vector2<T> operator+(const Vector2<T> &vec) const { | |||
return Vector2<T>(x_ + vec.x_, y_ + vec.y_); | |||
} | |||
Vector2<T> &operator+=(const Vector2<T> &vec) { | |||
x_ += vec.x_; | |||
y_ += vec.y_; | |||
return *this; | |||
} | |||
Vector2<T> operator-(const Vector2<T> &vec) const { | |||
return Vector2<T>(x_ - vec.x_, y_ - vec.y_); | |||
} | |||
Vector2<T> &operator-=(const Vector2<T> &vec) { | |||
x_ -= vec.x_; | |||
y_ -= vec.y_; | |||
return *this; | |||
} | |||
Vector2<T> operator*(const Vector2<T> &vec) const { | |||
return Vector2<T>(x_ * vec.x_, y_ * vec.y_); | |||
} | |||
Vector2<T> &operator*=(const Vector2<T> &vec) { | |||
x_ *= vec.x_; | |||
y_ *= vec.y_; | |||
return *this; | |||
} | |||
Vector2<T> operator*(T num) const { | |||
return Vector2<T>(x_ * num, y_ * num); | |||
} | |||
Vector2<T> operator*=(T num) { | |||
x_ *= num; | |||
y_ *= num; | |||
return *this; | |||
} | |||
Vector2<T> operator/(const Vector2<T> &vec) const { | |||
return Vector2<T>(x_ / vec.x_, y_ / vec.y_); | |||
} | |||
Vector2<T> &operator/=(const Vector2<T> &vec) { | |||
x_ /= vec.x_; | |||
y_ /= vec.y_; | |||
return *this; | |||
} | |||
Vector2<T> operator/(T num) const { | |||
return Vector2<T>(x_ / num, y_ / num); | |||
} | |||
Vector2<T> operator/=(T num) { | |||
x_ /= num; | |||
y_ /= num; | |||
return *this; | |||
} | |||
}; | |||
using Vec2 = Vector2<float>; | |||
} |
@@ -1,7 +1,9 @@ | |||
#pragma once | |||
#include <SFML/System/Vector2.hpp> | |||
#include <SFML/Graphics.hpp> | |||
#include <SFML/System/Vector2.hpp> | |||
#include "Vector2.h" | |||
namespace Swan { | |||
@@ -10,47 +12,6 @@ static constexpr int TICK_RATE = 20; | |||
static constexpr int CHUNK_HEIGHT = 32; | |||
static constexpr int CHUNK_WIDTH = 32; | |||
class Vec2: public sf::Vector2<float> { | |||
public: | |||
using sf::Vector2<float>::Vector2; | |||
Vec2 operator+(Vec2 &vec) { | |||
return Vec2(x + vec.x, y + vec.y); | |||
} | |||
Vec2 &operator+=(Vec2 &vec) { | |||
this->x += vec.x; | |||
this->y += vec.y; | |||
return *this; | |||
} | |||
Vec2 operator-(Vec2 &vec) { | |||
return Vec2(x - vec.x, y - vec.y); | |||
} | |||
Vec2 &operator-=(Vec2 &vec) { | |||
this->x -= vec.x; | |||
this->y -= vec.y; | |||
return *this; | |||
} | |||
Vec2 operator*(Vec2 &vec) { | |||
return Vec2(x * vec.x, y * vec.y); | |||
} | |||
Vec2 &operator*=(Vec2 &vec) { | |||
this->x *= vec.x; | |||
this->y *= vec.y; | |||
return *this; | |||
} | |||
Vec2 operator/(Vec2 &vec) { | |||
return Vec2(x / vec.x, y / vec.y); | |||
} | |||
Vec2 &operator/=(Vec2 &vec) { | |||
this->x /= vec.x; | |||
this->y /= vec.y; | |||
return *this; | |||
} | |||
}; | |||
struct Win { | |||
public: | |||
sf::RenderWindow *window_; |
@@ -11,15 +11,15 @@ void Body::gravity(Vec2 g) { | |||
} | |||
void Body::collide(WorldPlane &plane) { | |||
int startx = (int)pos_.x; | |||
int endx = (int)(pos_.x + size_.x); | |||
int startx = (int)pos_.x_; | |||
int endx = (int)(pos_.x_ + size_.x_); | |||
int y = (int)(pos_.y + size_.y); | |||
int y = (int)(pos_.y_ + size_.y_); | |||
for (int x = startx; x <= endx; ++x) { | |||
Tile &tile = plane.getTile(x, y); | |||
if (!tile.opts_.transparent_) { | |||
pos_.y = y - size_.y; | |||
vel_.y = 0; | |||
pos_.y_ = y - size_.y_; | |||
vel_.y_ = 0; | |||
} | |||
} | |||
} |