📄 mapobject.cpp
字号:
#include "MapObject.h"
#include "App.h"
#include "FixUtil.h"
#include "Map.h"
#include <io/InputStream.h>
#include <io/OutputStream.h>
#include <stdlib.h>
#include <config.h>
using namespace lang;
MapObject::MapObject() :
m_map( 0 ),
m_pos( Fix(0), Fix(0) ),
m_speed( 0 ),
m_hitpoints( 0 ),
m_maxHitPoints( 0 ),
m_sanity( 0 ),
m_isDrawn( false ),
m_prevPos( Fix(0), Fix(0) ),
m_dir( DIR_0 )
{
String::cpy( m_name, sizeof(m_name), "default" );
}
MapObject::MapObject( const char* name ) :
m_map( 0 ),
m_pos( Fix(0), Fix(0) ),
m_hitpoints( 0 ),
m_maxHitPoints( 0 ),
m_sanity( 0 ),
m_prevPos( Fix(0), Fix(0) ),
m_dir( DIR_0 )
{
String::cpy( m_name, sizeof(m_name), name );
}
MapObject::~MapObject()
{
}
void MapObject::read( io::InputStream* in )
{
char* begin = (char*) &MapObject_storeStart;
char* end = (char*) &MapObject_storeEnd;
in->read( begin, end-begin );
}
void MapObject::write( io::OutputStream* out )
{
char* begin = (char*) &MapObject_storeStart;
char* end = (char*) &MapObject_storeEnd;
out->write( begin, end-begin );
}
const char* MapObject::expandPath( const char* sz )
{
App& app = *App::get();
return app.expandPath( sz );
}
void MapObject::setSpeed( Fix speed )
{
m_speed = speed;
}
void MapObject::setHitPoints( Fix hp )
{
m_maxHitPoints = hp;
m_hitpoints = hp;
}
void MapObject::setSanity( Fix sanity )
{
m_sanity = sanity;
}
void MapObject::setName( const char* name )
{
String::cpy( m_name, sizeof(m_name), name );
}
void MapObject::setPosition( const FixVec2& pos )
{
m_pos = pos;
}
void MapObject::setBlockPosition( const FixVec2& pos )
{
m_pos = m_map->blockToPixel( pos + FixVec2(Fixf(0.5),Fixf(0.5)) );
}
FixVec2 MapObject::blockPosition() const
{
return m_map->pixelToBlock( m_pos );
}
FixVec2 MapObject::blockRenderPositionReference() const
{
return m_map->pixelToBlock( m_pos );
}
FixVec2 MapObject::blockRenderPositionExtreme() const
{
return m_map->pixelToBlock( m_pos );
}
void MapObject::setDirection( Dir dir )
{
m_dir = (char)dir;
}
FixVec2 MapObject::directionVector() const
{
int x = int(m_dir) * (Fix::SIN_SIZE/DIR_COUNT);
return FixVec2( Fix::cos(x), -Fix::sin(x) );
}
void MapObject::storePosition()
{
m_prevPos = m_pos;
}
void MapObject::restorePosition()
{
m_pos = m_prevPos;
}
MapObject::Dir MapObject::getAngleAsDir( Fix angle )
{
if ( angle.v < 0 )
angle += Fixf(360);
if ( angle >= Fixf(360-22.5) && angle <= Fixf(22.5) )
return DIR_0;
if ( angle >= Fixf(22.5+45.0*0) && angle <= Fixf(22.5+45.0*1) )
return DIR_315;
if ( angle >= Fixf(22.5+45.0*1) && angle <= Fixf(22.5+45.0*2) )
return DIR_270;
if ( angle >= Fixf(22.5+45.0*2) && angle <= Fixf(22.5+45.0*3) )
return DIR_225;
if ( angle >= Fixf(22.5+45.0*3) && angle <= Fixf(22.5+45.0*4) )
return DIR_180;
if ( angle >= Fixf(22.5+45.0*4) && angle <= Fixf(22.5+45.0*5) )
return DIR_135;
if ( angle >= Fixf(22.5+45.0*5) && angle <= Fixf(22.5+45.0*6) )
return DIR_90;
if ( angle >= Fixf(22.5+45.0*6) && angle <= Fixf(22.5+45.0*7) )
return DIR_45;
return DIR_0;
}
MapObject::Dir MapObject::getATanAsDir( const FixVec2& v )
{
Fix angle = Fix::atan2( v.y, v.x ) * Fix_radToDeg;
return getAngleAsDir( angle );
}
int MapObject::getObjects( const FixVec2& pos, Fix maxdist, Fix maxanglecos, MapObject** objlist, int maxobjs )
{
assert( m_map );
assert( maxobjs > 0 );
Fix maxdistsqr = maxdist * maxdist;
FixVec2 thisdir = directionVector();
int size = 0;
const int objcount = m_map->objects();
for ( int i = 0 ; i < objcount ; ++i )
{
MapObject* obj = m_map->getObject(i);
if ( obj != this )
{
FixVec2 distv = obj->position() - pos;
Fix distsqr = distv.dot( distv );
if ( distv.x.abs()+distv.y.abs() < maxdist && distsqr < maxdistsqr )
{
Fix dota = Fixf(1);
if ( distsqr > Fixf(0) )
{
dota = distv.dot( thisdir ) / Fix::sqrt( distsqr );
}
if ( dota > maxanglecos )
{
objlist[size++] = obj;
if ( size >= maxobjs )
break;
}
}
}
}
return size;
}
bool MapObject::isSkillSuccess( Fix level ) const
{
Fix rv( (rand()>>5) & 0xFF );
return ( rv < level );
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -