mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
Three changes to the platform logic, each as the client's platform does it: - Mover and rotator on one object: the object now turns at an angular velocity. A mover setting off (or going on between waypoints of different speeds) puts the object in the rotation for where it is and gives it the angular velocity that brings it to the next waypoint's rotation as it gets there. A rotator that has not stopped puts its own angular velocity back every frame, so the mover only turns the object while the rotator is stopped. Objects without a rotator still turn between the two waypoints' rotations as before. - Departed: a new script hook, OnDeparted, called when a platform leaves a waypoint it waited at, before it sets off. - Two waypoints in one place: a platform that has not stopped looks for its arrival every frame, also while it waits, and with no special case for a next waypoint in the same place. It is there at once, and on a path that keeps it at one waypoint it arrives every frame (without resending an unchanged state). ScriptComponent::SetScript can now take a script itself (used by the tests). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
3.4 KiB
C++
107 lines
3.4 KiB
C++
/*
|
|
* Darkflame Universe
|
|
* Copyright 2018
|
|
*/
|
|
|
|
#ifndef SCRIPTCOMPONENT_H
|
|
#define SCRIPTCOMPONENT_H
|
|
|
|
#include "CppScripts.h"
|
|
#include "Component.h"
|
|
#include "GameMessages.h"
|
|
#include <functional>
|
|
#include <map>
|
|
#include <string>
|
|
#include "eReplicaComponentType.h"
|
|
|
|
struct ObjectLoadState;
|
|
|
|
class Entity;
|
|
|
|
/**
|
|
* Handles the loading and execution of server side scripts on entities, scripts were originally written in Lua,
|
|
* here they're written in C++
|
|
*/
|
|
class ScriptComponent final : public Component {
|
|
public:
|
|
static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::SCRIPT;
|
|
|
|
// The client's allocator for this type (ObjectLoader2::DoObjectComponentLoad calls it in load order)
|
|
static void Allocator(Entity& entity, ObjectLoadState& state);
|
|
|
|
ScriptComponent(Entity* parent, const int32_t componentID, const std::string& scriptName, bool serialized, bool client = false);
|
|
|
|
// One the object's registry or level config gives (LWOScriptComponent's allocator): its script comes from the
|
|
// ScriptComponent row and custom_script_server / custom_script_client
|
|
ScriptComponent(Entity* parent, const int32_t componentID);
|
|
|
|
void LoadDataFromTemplate() override;
|
|
void LoadConfigData() override;
|
|
|
|
void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override;
|
|
|
|
/**
|
|
* Returns the script that's attached to this entity
|
|
* @return the script that's attached to this entity
|
|
*/
|
|
CppScripts::Script* const GetScript();
|
|
|
|
/**
|
|
* Sets whether the component is serialized (false: a server-only script the client has no component for)
|
|
* @param var whether the entity should be serialized
|
|
*/
|
|
void SetSerialized(const bool var) { m_Serialized = var; }
|
|
|
|
/**
|
|
* Sets the script using a path by looking through dScripts for a script that matches
|
|
* @param scriptName the name of the script to find
|
|
*/
|
|
void SetScript(const std::string& scriptName);
|
|
|
|
/**
|
|
* Sets the script itself (it is not owned: scripts are shared singletons)
|
|
*/
|
|
void SetScript(CppScripts::Script* const script) { m_Script = script; }
|
|
|
|
bool OnGetObjectReportInfo(GameMessages::GetObjectReportInfo& reportInfo);
|
|
|
|
// Registers a message from a script to be listened for on the parent object
|
|
template<typename ScriptClass, typename DerivedMsgType>
|
|
void RegisterMsg(ScriptClass* scriptThis, bool (ScriptClass::*scriptHandler)(Entity&, DerivedMsgType&)) {
|
|
static_assert(std::is_base_of_v<GameMessages::GameMsg, DerivedMsgType>, "DerivedMsgType must derive from GameMessages::GameMsg base class.");
|
|
const auto boundMsg = std::bind(scriptHandler, scriptThis, std::placeholders::_1, std::placeholders::_2);
|
|
auto* const parent = m_Parent;
|
|
const auto castWrapper = [parent, boundMsg](GameMessages::GameMsg& msg) {
|
|
return boundMsg(*parent, static_cast<DerivedMsgType&>(msg));
|
|
};
|
|
DerivedMsgType msg;
|
|
m_Parent->RegisterMsg(msg.msgId, castWrapper);
|
|
}
|
|
|
|
private:
|
|
|
|
/**
|
|
* The script attached to this entity
|
|
*/
|
|
CppScripts::Script* m_Script;
|
|
|
|
/**
|
|
* Whether the component is serialized (false: a server-only script the client has no component for)
|
|
*/
|
|
bool m_Serialized;
|
|
|
|
/**
|
|
* Whether or not this script is a client script
|
|
*/
|
|
bool m_Client;
|
|
|
|
std::string m_ScriptName;
|
|
|
|
// Made from the registry / level config: LoadDataFromTemplate and LoadConfigData pick the script
|
|
bool m_FromObject = false;
|
|
// The row's client script name (the client runs one when there is no server script)
|
|
std::string m_ClientScriptName;
|
|
};
|
|
|
|
#endif // SCRIPTCOMPONENT_H
|