Show worldpos.cpp syntax highlighted
/***************************************************************************
worldpos.cpp - description
-------------------
begin : Thu Nov 14 2002
copyright : (C) 2002 by Brendon Higgins
email : freepop-devel@lists.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <worldpos.h>
#include <mappos.h>
#include <maptilepos.h>
#include <rotation.h>
#include <ClanLib/network.h>
#include <cmath>
WorldPos::WorldPos() {
x = 0.0f;
y = 0.0f;
}
WorldPos::WorldPos(int mx, int my, float tx, float ty) {
x = mx + tx;
y = my + ty;
}
WorldPos::WorldPos(float wx, float wy) {
x = wx;
y = wy;
}
WorldPos::WorldPos(const WorldPos& p) {
x = p.x;
y = p.y;
}
WorldPos::WorldPos(const MapPos& p) {
x = p.getX();
y = p.getY();
}
WorldPos& WorldPos::operator=(const WorldPos& p) {
x = p.x;
y = p.y;
return *this;
}
float WorldPos::getX() const {
return x;
}
float WorldPos::getY() const {
return y;
}
void WorldPos::setX(float nx) {
x = nx;
}
void WorldPos::setY(float ny) {
y = ny;
}
int WorldPos::getMapX() const {
return int(x);
}
int WorldPos::getMapY() const {
return int(y);
}
float WorldPos::getTileX() const {
// Use floor instead of getMap* to avoid
// unnecessary float->int->float conversion
return x - std::floor(x);
}
float WorldPos::getTileY() const {
// Use floor instead of getMap* to avoid
// unnecessary float->int->float conversion
return y - std::floor(y);
}
bool WorldPos::sameTile(const MapPos& p) const {
return getMapX() == p.getX() && getMapY() == p.getY();
}
bool WorldPos::near(const WorldPos& pos, float rangeSq) const {
// Short circuit a couple of things to try to avoid float multiplies.
// When range >= 1.0, rangeSq >= range, so if d > rangeSq it's > range.
float dx = std::abs(x - pos.x);
if (dx >= 1.0f && dx > rangeSq) return false;
float dy = std::abs(y - pos.y);
if (dy >= 1.0f && dy > rangeSq) return false;
return (dx * dx + dy * dy) <= rangeSq;
}
bool WorldPos::operator==(const WorldPos& p) const {
return p.x == x && p.y == y;
}
WorldPos& WorldPos::operator+=(const WorldPos& p) {
x += p.x;
y += p.y;
return *this;
}
WorldPos& WorldPos::operator-=(const WorldPos& p) {
x -= p.x;
y -= p.y;
return *this;
}
void WorldPos::normalise() {
if (x != 0.0f || y != 0.0f) {
*this /= std::sqrt(x * x + y * y);
}
}
WorldPos& WorldPos::operator*=(float n) {
x *= n;
y *= n;
return *this;
}
WorldPos& WorldPos::operator/=(float n) {
x /= n;
y /= n;
return *this;
}
float WorldPos::distanceSquared(const WorldPos& p) const {
float dx = x - p.x;
float dy = y - p.y;
return dx * dx + dy * dy;
}
MapTilePos WorldPos::getTile() const {
return MapTilePos(getTileX(), getTileY());
}
MapPos WorldPos::getMap() const {
return MapPos(getMapX(), getMapY());
}
void WorldPos::rotate(const Rotation& r, int width, int height) {
float ox = x;
float oy = y;
switch (r.getClock()) {
case 1:
x = height - oy;
y = ox;
break;
case 2:
x = width - ox;
y = height - oy;
break;
case 3:
x = oy;
y = width - ox;
break;
default:
break;
}
}
std::pair<MapPos, MapTilePos> WorldPos::split() const {
// TODO Can this be made more efficient?
return std::make_pair(getMap(), getTile());
}
void WorldPos::inject(CL_OutputSource& os) const {
os.write_float32(x);
os.write_float32(y);
}
WorldPos WorldPos::extract(CL_InputSource& is) {
// Put these straight in the constructor and they tend to get swapped.
// This is because the order of operations to calculate parameters is
// undefined in C++.
float x = is.read_float32();
float y = is.read_float32();
return WorldPos(x, y);
}
WorldPos operator+(const MapPos& mp, const MapTilePos& tp) {
return WorldPos(mp.getX(), mp.getY(), tp.getX(), tp.getY());
}
std::ostream& operator<<(std::ostream& os, const WorldPos& p) {
return os << '(' << p.getX() << ',' << p.getY() << ')';
}
See more files for this project here