Show townclient.cpp syntax highlighted
/***************************************************************************
townclient.cpp
-------------------
begin : Wed Jul 14 2004
copyright : (C) 2004 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 <town.h>
#include <townclient.h>
#include <maptilepos.h>
#include <worldclient.h>
#include <worldpos.h>
#include <client.h>
#include <clientmisc.h>
#include <oversprite.h>
#include <playerclient.h>
#include <peepmagnetclient.h>
TownClient::TownClient(WorldClient* w, const Identity& ident, CL_NetPacket &p,
int reason):
EntityClient(ident),
sprite(0) {
onFullUpdate(w, p);
}
TownClient::~TownClient() {
delete sprite;
}
WorldPos TownClient::getPos() const {
return pos + MapTilePos(0.5f, 0.5f);
}
void TownClient::onFullUpdate(WorldClient* w, CL_NetPacket& p) {
pos = MapPos::extract(p.input);
ownerSlot = p.input.read_int32();
strength = p.input.read_float32();
state = p.input.read_int32();
hasLeader = p.input.read_bool8();
setSpriteState(w);
}
void TownClient::onMessage(WorldClient* w, int m, CL_NetPacket& p) {
if (m == Town::Message::STRENGTH) onStrength(p);
if (m == Town::Message::STATE) onState(w, p);
}
void TownClient::draw(const WorldClient* w) const {
CL_Point gp = w->getMap()->findPos(getPos());
sprite->draw(gp.x, gp.y);
if (hasLeader) {
// TODO: Offset not builtin
// HACK, A little bit of a
PeepMagnetClient::spriteMiniCache->set_color(
intToCL_Color(w->findPlayer(ownerSlot)->getColour()));
PeepMagnetClient::spriteMiniCache->draw(gp.x +15, gp.y -30);
}
}
void TownClient::playerColourChange(WorldClient* w) {
sprite->set_color(intToCL_Color(w->findPlayer(ownerSlot)->getColour()));
}
void TownClient::displayRotationChange(WorldClient* w, const Rotation& r) {
setSpriteState(w);
}
void TownClient::onStrength(CL_NetPacket& p) {
strength = p.input.read_float32();
}
void TownClient::onState(WorldClient* w, CL_NetPacket& p) {
state = p.input.read_int32();
strength = p.input.read_float32();
hasLeader = p.input.read_bool8();
setSpriteState(w);
}
void TownClient::setSpriteState(const WorldClient* w) {
delete sprite;
std::string sn = "entities/towns/human/1/";
if (state == Town::State::BURNING) {
sn += "burning";
} else if (state == Town::State::DEAD) {
sn += "dead";
} else {
sn += "normal";
}
sn += "/dir";
sn += char(w->getDisplayRotation().getClock()) + '0';
sprite = new OverSprite(sn, app.getResources(), intToCL_Color(
w->findPlayer(ownerSlot)->getColour()));
}
See more files for this project here