Initial boat support

+ Boats are saved
+ Boats have physics
+ Boats spawn
master
Tiger Wang 2013-09-08 00:14:57 +01:00
parent f300ed54e5
commit c789a8ddf5
8 changed files with 217 additions and 1 deletions

84
source/Entities/Boat.cpp Normal file
View File

@ -0,0 +1,84 @@
// Minecart.cpp
// Implements the cMinecart class representing a minecart in the world
// Indiana Jones!
#include "Globals.h"
#include "Boat.h"
#include "../World.h"
#include "../ClientHandle.h"
#include "Player.h"
cBoat::cBoat(double a_X, double a_Y, double a_Z) :
super(etBoat, a_X, a_Y, a_Z, 0.98, 0.7)
{
SetMass(20.f);
SetMaxHealth(6);
SetHealth(6);
}
void cBoat::SpawnOn(cClientHandle & a_ClientHandle)
{
a_ClientHandle.SendSpawnVehicle(*this, 1, 0);
}
void cBoat::DoTakeDamage(TakeDamageInfo & TDI)
{
super::DoTakeDamage(TDI);
if (GetHealth() == 0)
{
Destroy(true);
}
}
void cBoat::OnRightClicked(cPlayer & a_Player)
{
if (m_Attachee != NULL)
{
if (m_Attachee->GetUniqueID() == a_Player.GetUniqueID())
{
// This player is already sitting in, they want out.
a_Player.Detach();
return;
}
if (m_Attachee->IsPlayer())
{
// Another player is already sitting in here, cannot attach
return;
}
// Detach whatever is sitting in this minecart now:
m_Attachee->Detach();
}
// Attach the player to this minecart
a_Player.AttachTo(this);
}
void cBoat::HandlePhysics(float a_Dt, cChunk & a_Chunk)
{
super::HandlePhysics(a_Dt, a_Chunk);
BroadcastMovementUpdate();
}

34
source/Entities/Boat.h Normal file
View File

@ -0,0 +1,34 @@
// Boat.h
// Declares the cBoat class representing a minecart in the world
#pragma once
#include "Entity.h"
#include "../Item.h"
class cBoat :
public cEntity
{
typedef cEntity super;
public:
CLASS_PROTODEF(cBoat);
// cEntity overrides:
virtual void SpawnOn(cClientHandle & a_ClientHandle) override;
virtual void OnRightClicked(cPlayer & a_Player) override;
virtual void DoTakeDamage(TakeDamageInfo & TDI) override;
virtual void HandlePhysics(float a_Dt, cChunk & a_Chunk) override;
cBoat(double a_X, double a_Y, double a_Z);
} ;

54
source/Items/ItemBoat.h Normal file
View File

@ -0,0 +1,54 @@
// ItemBoat.h
// Declares the various boat ItemHandlers
#pragma once
#include "../Entities/Boat.h"
class cItemBoatHandler :
public cItemHandler
{
typedef cItemHandler super;
public:
cItemBoatHandler(int a_ItemType) :
super(a_ItemType)
{
}
virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Dir) override
{
if (a_Dir < 0)
{
return false;
}
double x = (double)a_BlockX + 0.5;
double y = (double)a_BlockY + 0.5;
double z = (double)a_BlockZ + 0.5;
cBoat * Boat = NULL;
Boat = new cBoat (x, y, z);
Boat->Initialize(a_World);
return true;
}
} ;

View File

@ -8,6 +8,7 @@
// Handlers:
#include "ItemBed.h"
#include "ItemBoat.h"
#include "ItemBow.h"
#include "ItemBrewingStand.h"
#include "ItemBucket.h"
@ -180,6 +181,11 @@ cItemHandler *cItemHandler::CreateItemHandler(int a_ItemType)
return new cItemMinecartHandler(a_ItemType);
}
case E_ITEM_BOAT:
{
return new cItemBoatHandler(a_ItemType);
}
// Food:
case E_ITEM_BREAD:
case E_ITEM_COOKIE:

View File

@ -19,6 +19,7 @@
#include "../OSSupport/MakeDir.h"
#include "FastNBT.h"
#include "../Entities/FallingBlock.h"
#include "../Entities/Boat.h"
#include "../Entities/Minecart.h"
#include "../Mobs/Monster.h"
#include "../Entities/Pickup.h"
@ -252,6 +253,20 @@ void cNBTChunkSerializer::AddBasicEntity(cEntity * a_Entity, const AString & a_C
void cNBTChunkSerializer::AddBoatEntity(cBoat * a_Boat)
{
const char * EntityClass = NULL;
EntityClass = "Boat";
m_Writer.BeginCompound("");
AddBasicEntity(a_Boat, EntityClass);
m_Writer.EndCompound();
}
void cNBTChunkSerializer::AddFallingBlockEntity(cFallingBlock * a_FallingBlock)
{
m_Writer.BeginCompound("");
@ -461,6 +476,7 @@ void cNBTChunkSerializer::Entity(cEntity * a_Entity)
switch (a_Entity->GetEntityType())
{
case cEntity::etBoat: AddBoatEntity ((cBoat *) a_Entity); break;
case cEntity::etFallingBlock: AddFallingBlockEntity((cFallingBlock *) a_Entity); break;
case cEntity::etMinecart: AddMinecartEntity ((cMinecart *) a_Entity); break;
case cEntity::etMonster: AddMonsterEntity ((cMonster *) a_Entity); break;

View File

@ -19,6 +19,7 @@
class cFastNBTWriter;
class cEntity;
class cBlockEntity;
class cBoat;
class cChestEntity;
class cDispenserEntity;
class cDropperEntity;
@ -94,6 +95,7 @@ protected:
// Entities:
void AddBasicEntity (cEntity * a_Entity, const AString & a_ClassName);
void AddBoatEntity (cBoat * a_Boat);
void AddFallingBlockEntity(cFallingBlock * a_FallingBlock);
void AddMinecartEntity (cMinecart * a_Minecart);
void AddMonsterEntity (cMonster * a_Monster);

View File

@ -23,6 +23,7 @@
#include "../OSSupport/MakeDir.h"
#include "FastNBT.h"
#include "../Mobs/Monster.h"
#include "../Entities/Boat.h"
#include "../Entities/FallingBlock.h"
#include "../Entities/Minecart.h"
#include "../Entities/Pickup.h"
@ -911,7 +912,11 @@ void cWSSAnvil::LoadSignFromNBT(cBlockEntityList & a_BlockEntities, const cParse
void cWSSAnvil::LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, int a_IDTagLength)
{
if (strncmp(a_IDTag, "FallingBlock", a_IDTagLength) == 0)
if (strncmp(a_IDTag, "Boat", a_IDTagLength) == 0)
{
LoadBoatFromNBT(a_Entities, a_NBT, a_EntityTagIdx);
}
else if (strncmp(a_IDTag, "FallingBlock", a_IDTagLength) == 0)
{
LoadFallingBlockFromNBT(a_Entities, a_NBT, a_EntityTagIdx);
}
@ -967,6 +972,20 @@ void cWSSAnvil::LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a
void cWSSAnvil::LoadBoatFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx)
{
std::auto_ptr<cBoat> Boat(new cBoat(0, 0, 0));
if (!LoadEntityBaseFromNBT(*Boat.get(), a_NBT, a_TagIdx))
{
return;
}
a_Entities.push_back(Boat.release());
}
void cWSSAnvil::LoadFallingBlockFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx)
{
// TODO

View File

@ -138,6 +138,7 @@ protected:
void LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, int a_IDTagLength);
void LoadBoatFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIndx);
void LoadFallingBlockFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx);
void LoadMinecartRFromNBT (cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx);
void LoadMinecartCFromNBT (cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_TagIdx);