RE: Destructible object ineisis online
Anyways my EE 1.0 Inesis based game used this code, I haven't made destructibles work in EE2+
Code:
/******************************************************************************/
class TimedDestructible : Game.Destructible
{
Flt creation_time=0.0; // time we made this
Flt life_time; // how long we should stay before fading away
MaterialPtr mptr = NULL;
void create(C Str file, C VecI &blockpos, C Vec &impulse, C MaterialPtr &material)
{
Box b(Vec(0),Vec(1)); b+=blockpos;
tactor.create(b, 1).mass(0.1).ccd(true); // make a temporary actor with that matches the blocks matrix
T.actors.clear();
Game::ObjParamsPtr obj6=Game::Objs.ptrRequire(file);
Game.ObjParams obj7= *obj6;
super.create(obj7); T.matrix(Matrix(tactor.matrix().normalize())); // this creates the destructible object with the tactor's matrix
REPA(T.actors)
{
actors[i].sleep(false).group(AG_BLOCK_SHARD).user(this); // unsleep the shard's physics, set their usergroup and userptr, need this for when we pick them up
}
REPA(T.actors)actors[i].addImpulse(Vec(RandomF()),blockpos); // add an impulse so they fly apart
//if(destruct_mesh)for(int i = 0; i<destruct_mesh.parts(); i++)destruct_mesh.part(i).mesh.setMaterial(material); // actually we use material lock when we draw the fragments using mptr, so this line is probably not needed.
mptr =material;
creation_time = Time.realTime();
tactor.del();
}
}
void UpdateTimedDestructibles()
{
REPA(timeddestr)
{
if(Time.realTime() - timeddestr[i].creation_time > 10.0)
{
timeddestr.removeValid(i);
return;
}
}
}
Actor tactor;
Memx<TimedDestructible> timeddestr;
call UpdateTimedDestructibles(); in your game update
Code:
void ReportContact(ActorInfo &contact_a, ActorInfo &contact_b, C PhysContact *contact, Int contacts)
{
if(Kb.b(KB_C)) // if we are holding down the 'collect key' and we have made physcial contact, delete the shards
{
// CM.New(S+"Collected Shard "+contact_a.group+" "+contact_b.group);
REPAD(i,timeddestr)
REPAD(j,timeddestr[i].actors)
if(timeddestr[i].actors[j].user() == contact_a.user)
{
CM.New(S+"You collect a shard.");
timeddestr.removeValid(i); //CM.New("Removed a shard.");
return;
}
}
}
this was in rendergame(), I have commented it out:
Code:
// EE engine update removed material lock, maybe this isn't possible anymore?
/* REPA(timeddestr)for(int j = 0; j<timeddestr[i].destruct_mesh.parts(); j++)
{
MaterialLock = &(*timeddestr[i].mptr);
timeddestr[i].destruct_mesh.part(j).mesh.draw(timeddestr[i].actors[j].matrix());
MaterialLock = NULL;
}*/
then to communicate from cleint/server so that all users see blocks breaking and disappearing when they are picked up:
on client
Code:
case CS_DESTRUCTIBLE_SPAWN:
{
Str objfile; VecI blockpos; Vec impulse; Str material;
ClientRecvDestructibleSpawn(Server.data, objfile, blockpos, impulse, material);
MaterialPtr mptr; mptr = material;
// EE 2.0 broke destructibles!
//timeddestr.New().create(objfile, blockpos, impulse, mptr);
//CM.New("Recieved CS_DESTRUCTIBLE_SPAWN");
destroy_particles.New().create(blockpos, Vec(0,1, 0));
} break;
Code:
/******************************************************************************/
// CS_DESTRUCTIBLE_SPAWN
/******************************************************************************/
void ClientSendDestructibleSpawn(Connection &conn, C Str &objfile, C VecI &blockpos, C Vec &impulse, C MaterialPtr &material)
{
//File f; f.writeMem().putByte(CS_DESTRUCTIBLE_SPAWN).putInt(spell).putStr(target); f.pos(0); conn.dataFull(f, f.left());
File f; f.writeMem().putByte(CS_DESTRUCTIBLE_SPAWN);
f.putStr(objfile);
f.put(&blockpos, SIZE(VecI));
f.put(&impulse, SIZE(Vec));
f.putStr(material.name());
//Gui.msgBox("SendCS_DESTRUCTIBLE_SPAWN","SendCS_DESTRUCTIBLE_SPAWN");
f.pos(0); conn.send(f, f.left());
}
void ServerRecvDestructibleSpawn(File &f, Str &objfile, VecI &blockpos, Vec &impulse, Str &material)
{
// Gui.msgBox("RecvCS_DESTRUCTIBLE_SPAWN","RecvCS_DESTRUCTIBLE_SPAWN");
objfile = f.getStr();
f.get(&blockpos, SIZE(VecI));
f.get(&impulse, SIZE(Vec));
material = f.getStr();
}
void ServerWriteDestructibleSpawn(File &f, C Str &objfile, C VecI &blockpos, C Vec &impulse, C Str &material)
{
//Gui.msgBox("SendCS_DESTRUCTIBLE_SPAWN","SendCS_DESTRUCTIBLE_SPAWN");
f.writeMem().putByte(CS_DESTRUCTIBLE_SPAWN);
f.putStr(objfile);
f.put(&blockpos, SIZE(VecI));
f.put(&impulse, SIZE(Vec));
f.putStr(material);
}
void ClientRecvDestructibleSpawn(File &f, Str &objfile, VecI &blockpos, Vec &impulse, Str &material)
{
//Gui.msgBox("RecvCS_DESTRUCTIBLE_SPAWN","RecvCS_DESTRUCTIBLE_SPAWN");
objfile = f.getStr();
f.get(&blockpos, SIZE(VecI));
f.get(&impulse, SIZE(Vec));
material = f.getStr();
}
on server:
Code:
case CS_DESTRUCTIBLE_SPAWN:
{
Str objfile; VecI blockpos; Vec impulse; Str material;
ServerRecvDestructibleSpawn(connection.data, objfile, blockpos, impulse, material);
File f; ServerWriteDestructibleSpawn(f, objfile, blockpos, impulse, material);
if(world())if(Area *data=GetArea(*world(),BlockToArea(blockpos)))
{
REPA(data.clients)
{
data.clients[i].send(f);
}
}
}break;
|