Code Search for Developers
 
 
  

worldpos.cpp from FreePop at Krugle


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

FreePop

FreePop is a multi-platform tile-based game based on the great old game Populous 2 by Bullfrog Productions Ltd., but much improved.

Project homepage: http://sourceforge.net/projects/freepop
Programming language(s): C++
License: other

  client/
    Makefile.am
    client.cpp
    client.h
    clientmisc.cpp
    clientmisc.h
    clientstate.cpp
    clientstate.h
    connectstate.cpp
    connectstate.h
    cropsclient.cpp
    cropsclient.h
    entityclient.cpp
    entityclient.h
    entityclientfactory.cpp
    entityclientfactory.h
    firecolumnclient.cpp
    firecolumnclient.h
    gamestate.cpp
    gamestate.h
    loadstate.cpp
    loadstate.h
    mapclient.cpp
    mapclient.h
    maptileclient.cpp
    maptileclient.h
    messagebox.cpp
    messagebox.h
    oversprite.cpp
    oversprite.h
    paintable.cpp
    paintable.h
    pausefader.cpp
    pausefader.h
    peepclient.cpp
    peepclient.h
    peepmagnetclient.cpp
    peepmagnetclient.h
    playerclient.cpp
    playerclient.h
    playeroptionsdialog.cpp
    playeroptionsdialog.h
    rockclient.cpp
    rockclient.h
    swampclient.cpp
    swampclient.h
    townclient.cpp
    townclient.h
    treeclient.cpp
    treeclient.h
    worldclient.cpp
    worldclient.h
  server/
    Makefile.am
    contagion.cpp
    contagion.h
  Makefile.am
  common.cpp
  common.h
  corner.cpp
  corner.h
  crops.cpp
  crops.h
  entity.cpp
  entity.h
  entityfactory.cpp
  entityfactory.h
  firecolumn.cpp
  firecolumn.h
  gridmap.h
  identity.cpp
  identity.h
  map.cpp
  map.h
  mappos.cpp
  mappos.h
  maptile.cpp
  maptile.h
  maptilepos.cpp
  maptilepos.h
  misc.h
  peep.cpp
  peep.h
  peepmagnet.cpp
  peepmagnet.h
  player.cpp
  player.h
  rock.cpp
  rock.h
  rotation.cpp
  rotation.h
  rules.cpp
  rules.h
  slope.cpp
  slope.h
  swamp.cpp
  swamp.h
  tempo.cpp
  tempo.h
  town.cpp
  town.h
  tree.cpp
  tree.h
  worldpos.cpp
  worldpos.h