2014-10-02 12:44:12 +03:00
|
|
|
#include "core/log.h"
|
|
|
|
#include "client_file/api.h"
|
|
|
|
#include "network/api.h"
|
|
|
|
#include "entitysync/api.h"
|
|
|
|
#include "interface/module.h"
|
|
|
|
#include "interface/server.h"
|
|
|
|
#include "interface/event.h"
|
|
|
|
#include <Scene.h>
|
|
|
|
#include <RigidBody.h>
|
|
|
|
#include <CollisionShape.h>
|
|
|
|
#include <ResourceCache.h>
|
|
|
|
#include <Context.h>
|
|
|
|
#include <StaticModel.h>
|
|
|
|
#include <Model.h>
|
2014-10-02 18:54:31 +03:00
|
|
|
#include <Material.h>
|
2014-10-02 21:00:45 +03:00
|
|
|
#include <Texture2D.h>
|
|
|
|
#include <Technique.h>
|
2014-10-02 12:44:12 +03:00
|
|
|
#include <cereal/archives/portable_binary.hpp>
|
|
|
|
#include <cereal/types/unordered_map.hpp>
|
|
|
|
#include <cereal/types/vector.hpp>
|
|
|
|
|
|
|
|
namespace entitytest {
|
|
|
|
|
|
|
|
using interface::Event;
|
|
|
|
using namespace Urho3D;
|
|
|
|
|
|
|
|
struct Module: public interface::Module
|
|
|
|
{
|
|
|
|
interface::Server *m_server;
|
2014-10-03 01:45:29 +03:00
|
|
|
uint m_slow_count = 0;
|
2014-10-02 12:44:12 +03:00
|
|
|
|
|
|
|
Module(interface::Server *server):
|
|
|
|
interface::Module("entitytest"),
|
|
|
|
m_server(server)
|
|
|
|
{
|
|
|
|
log_v(MODULE, "entitytest construct");
|
|
|
|
}
|
|
|
|
|
|
|
|
~Module()
|
|
|
|
{
|
|
|
|
log_v(MODULE, "entitytest destruct");
|
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
log_v(MODULE, "entitytest init");
|
|
|
|
m_server->sub_event(this, Event::t("core:start"));
|
|
|
|
m_server->sub_event(this, Event::t("core:tick"));
|
|
|
|
m_server->sub_event(this, Event::t("network:new_client"));
|
|
|
|
m_server->sub_event(this, Event::t("network:client_disconnected"));
|
|
|
|
m_server->sub_event(this, Event::t("client_file:files_transmitted"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void event(const Event::Type &type, const Event::Private *p)
|
|
|
|
{
|
|
|
|
EVENT_VOIDN("core:start", on_start)
|
|
|
|
EVENT_TYPEN("core:tick", on_tick, interface::TickEvent)
|
|
|
|
EVENT_TYPEN("network:new_client", on_new_client, network::NewClient)
|
|
|
|
EVENT_TYPEN("network:client_disconnected", on_client_disconnected,
|
|
|
|
network::OldClient)
|
|
|
|
EVENT_TYPEN("client_file:files_transmitted", on_files_transmitted,
|
|
|
|
client_file::FilesTransmitted)
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_start()
|
|
|
|
{
|
|
|
|
m_server->access_scene([&](Scene *scene)
|
|
|
|
{
|
|
|
|
Context *context = scene->GetContext();
|
|
|
|
ResourceCache* cache = context->GetSubsystem<ResourceCache>();
|
2014-10-02 21:00:45 +03:00
|
|
|
auto *m = cache->GetResource<Material>("Materials/Stone.xml");
|
2014-10-03 01:45:29 +03:00
|
|
|
// NOTE: Modified or created materials will not be replicated to the
|
|
|
|
// client. Make sure to always have a resource file or create
|
|
|
|
// the material on the client.
|
|
|
|
/*m->SetTexture(TU_DIFFUSE,
|
|
|
|
cache->GetResource<Texture2D>("main/green_texture.png"));*/
|
2014-10-02 21:00:45 +03:00
|
|
|
/*Material *m = new Material(context);
|
|
|
|
m->SetTexture(TU_DIFFUSE,
|
|
|
|
cache->GetResource<Texture2D>("main/green_texture.png"));
|
|
|
|
m->SetTechnique(0, cache->GetResource<Technique>(
|
|
|
|
"Techniques/Diff.xml"));*/
|
2014-10-02 12:44:12 +03:00
|
|
|
|
2014-10-02 19:11:36 +03:00
|
|
|
{
|
|
|
|
Node* node = scene->CreateChild("DirectionalLight");
|
|
|
|
node->SetDirection(Vector3(-0.6f, -1.0f, 0.8f));
|
|
|
|
Light* light = node->CreateComponent<Light>();
|
|
|
|
light->SetLightType(LIGHT_DIRECTIONAL);
|
|
|
|
}
|
2014-10-02 12:44:12 +03:00
|
|
|
{
|
|
|
|
Node *n = scene->CreateChild("Plane");
|
|
|
|
n->SetPosition(Vector3(0.0f, -0.5f, 0.0f));
|
2014-10-02 19:11:36 +03:00
|
|
|
n->SetScale(Vector3(10.0f, 1.0f, 10.0f));
|
2014-10-02 12:44:12 +03:00
|
|
|
RigidBody *body = n->CreateComponent<RigidBody>();
|
|
|
|
CollisionShape *shape = n->CreateComponent<CollisionShape>();
|
|
|
|
shape->SetBox(Vector3::ONE);
|
2014-10-02 19:11:36 +03:00
|
|
|
body->SetFriction(0.75f);
|
2014-10-02 18:54:31 +03:00
|
|
|
StaticModel *object = n->CreateComponent<StaticModel>();
|
|
|
|
object->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
|
2014-10-02 21:00:45 +03:00
|
|
|
object->SetMaterial(m);
|
2014-10-02 12:44:12 +03:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Node *n = scene->CreateChild("Box");
|
2014-10-02 19:11:36 +03:00
|
|
|
n->SetPosition(Vector3(0.0f, 6.0f, 0.0f));
|
2014-10-02 12:44:12 +03:00
|
|
|
n->SetScale(Vector3(1.0f, 1.0f, 1.0f));
|
|
|
|
RigidBody *body = n->CreateComponent<RigidBody>();
|
|
|
|
CollisionShape *shape = n->CreateComponent<CollisionShape>();
|
|
|
|
shape->SetBox(Vector3::ONE);
|
|
|
|
body->SetMass(1.0);
|
2014-10-02 19:16:19 +03:00
|
|
|
body->SetFriction(0.75f);
|
|
|
|
//body->SetUseGravity(true);
|
|
|
|
//body->SetGravityOverride(Vector3(0.0, -1.0, 0.0));
|
2014-10-03 01:45:29 +03:00
|
|
|
/*StaticModel *object = n->CreateComponent<StaticModel>();
|
2014-10-02 19:16:19 +03:00
|
|
|
object->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
|
2014-10-03 01:45:29 +03:00
|
|
|
object->SetMaterial(m);*/
|
2014-10-02 19:16:19 +03:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Node *n = scene->CreateChild("Box2");
|
|
|
|
n->SetPosition(Vector3(-0.4f, 8.0f, 0.0f));
|
|
|
|
n->SetScale(Vector3(1.0f, 1.0f, 1.0f));
|
|
|
|
RigidBody *body = n->CreateComponent<RigidBody>();
|
|
|
|
CollisionShape *shape = n->CreateComponent<CollisionShape>();
|
|
|
|
shape->SetBox(Vector3::ONE);
|
|
|
|
body->SetMass(1.0);
|
|
|
|
body->SetFriction(0.75f);
|
2014-10-02 12:44:12 +03:00
|
|
|
//body->SetUseGravity(true);
|
|
|
|
//body->SetGravityOverride(Vector3(0.0, -1.0, 0.0));
|
2014-10-02 19:11:36 +03:00
|
|
|
StaticModel *object = n->CreateComponent<StaticModel>();
|
|
|
|
object->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
|
2014-10-02 21:00:45 +03:00
|
|
|
object->SetMaterial(m);
|
2014-10-02 12:44:12 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_tick(const interface::TickEvent &event)
|
|
|
|
{
|
|
|
|
log_d(MODULE, "entitytest::on_tick");
|
2014-10-02 19:11:36 +03:00
|
|
|
static uint a = 0;
|
|
|
|
if(((a++) % 25) == 0){
|
|
|
|
m_server->access_scene([&](Scene *scene){
|
|
|
|
Node *n = scene->GetChild("Box");
|
|
|
|
n->SetPosition(Vector3(0.0f, 6.0f, 0.0f));
|
|
|
|
n->SetRotation(Quaternion(30, 60, 90));
|
2014-10-02 19:16:19 +03:00
|
|
|
Node *n2 = scene->GetChild("Box2");
|
|
|
|
n2->SetPosition(Vector3(-0.4f, 8.0f, 0.0f));
|
|
|
|
n2->SetRotation(Quaternion(30, 60, 90));
|
2014-10-03 01:45:29 +03:00
|
|
|
m_slow_count++;
|
|
|
|
if(m_slow_count == 2){
|
|
|
|
Node *n = scene->CreateChild("Box");
|
|
|
|
n->SetPosition(Vector3(0.0f, 10.0f, 0.0f));
|
|
|
|
n->SetScale(Vector3(1.0f, 1.0f, 1.0f));
|
|
|
|
RigidBody *body = n->CreateComponent<RigidBody>();
|
|
|
|
CollisionShape *shape = n->CreateComponent<CollisionShape>();
|
|
|
|
shape->SetBox(Vector3::ONE);
|
|
|
|
body->SetMass(1.0);
|
|
|
|
body->SetFriction(0.75f);
|
|
|
|
}
|
2014-10-02 19:11:36 +03:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_server->access_scene([&](Scene *scene){
|
2014-10-02 12:44:12 +03:00
|
|
|
Node *n = scene->GetChild("Box");
|
|
|
|
//n->Translate(Vector3(0.1f, 0, 0));
|
|
|
|
Vector3 p = n->GetPosition();
|
|
|
|
log_v(MODULE, "Position: %s", p.ToString().CString());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_new_client(const network::NewClient &new_client)
|
|
|
|
{
|
|
|
|
log_i(MODULE, "entitytest::on_new_client: id=%zu", new_client.info.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_client_disconnected(const network::OldClient &old_client)
|
|
|
|
{
|
|
|
|
log_i(MODULE, "entitytest::on_client_disconnected: id=%zu", old_client.info.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_files_transmitted(const client_file::FilesTransmitted &event)
|
|
|
|
{
|
|
|
|
log_v(MODULE, "on_files_transmitted(): recipient=%zu", event.recipient);
|
2014-10-02 13:52:48 +03:00
|
|
|
|
|
|
|
network::access(m_server, [&](network::Interface * inetwork){
|
|
|
|
inetwork->send(event.recipient, "core:run_script",
|
|
|
|
"buildat.run_script_file(\"main/init.lua\")");
|
|
|
|
});
|
2014-10-02 12:44:12 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
EXPORT void* createModule_main(interface::Server *server){
|
|
|
|
return (void*)(new Module(server));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// vim: set noet ts=4 sw=4:
|