1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
|
#if defined(WITH_ANGELSCRIPT)
#include "ScriptActorWrapper.h"
#include "ScriptPlayerWrapper.h"
#include "LevelScriptLoader.h"
#include "RegisterArray.h"
#include "RegisterRef.h"
#include "../ILevelHandler.h"
#include "../Events/EventSpawner.h"
#include "../Actors/Explosion.h"
#include "../Actors/Player.h"
#include "../Actors/Weapons/ShotBase.h"
#include "../Actors/Weapons/TNT.h"
#include "../Actors/Enemies/TurtleShell.h"
#include "../../nCine/Base/FrameTimer.h"
#include "../../nCine/Base/Random.h"
using namespace Jazz2::Actors;
using namespace Jazz2::Tiles;
using namespace nCine;
#define AsClassName "ActorBase"
#define AsClassNameInternal "ActorBaseInternal"
namespace Jazz2::Scripting
{
ScriptActorWrapper::ScriptActorWrapper(LevelScriptLoader* levelScripts, asIScriptObject* obj)
: _levelScripts(levelScripts), _obj(obj), _refCount(1), _scoreValue(0)
{
_isDead = obj->GetWeakRefFlag();
_isDead->AddRef();
_onTileDeactivated = _obj->GetObjectType()->GetMethodByDecl("bool OnTileDeactivated()");
_onHealthChanged = _obj->GetObjectType()->GetMethodByDecl("void OnHealthChanged()");
_onUpdate = _obj->GetObjectType()->GetMethodByDecl("void OnUpdate(float)");
_onUpdateHitbox = _obj->GetObjectType()->GetMethodByDecl("void OnUpdateHitbox()");
_onHandleCollision = _obj->GetObjectType()->GetMethodByDecl("bool OnHandleCollision(ref other)");
_onHitFloor = _obj->GetObjectType()->GetMethodByDecl("void OnHitFloor(float)");
_onHitCeiling = _obj->GetObjectType()->GetMethodByDecl("void OnHitCeiling(float)");
_onHitWall = _obj->GetObjectType()->GetMethodByDecl("void OnHitWall(float)");
_onAnimationStarted = _obj->GetObjectType()->GetMethodByDecl("void OnAnimationStarted()");
_onAnimationFinished = _obj->GetObjectType()->GetMethodByDecl("void OnAnimationFinished()");
}
ScriptActorWrapper::~ScriptActorWrapper()
{
// This had to be added to release the object properly
_obj->Release();
_isDead->Release();
}
void ScriptActorWrapper::RegisterFactory(asIScriptEngine* engine, asIScriptModule* module)
{
static const char AsLibrary[] = R"(
shared abstract class )" AsClassName R"(
{
// Allow scripts to create instances
)" AsClassName R"(()
{
// Create the C++ side of the proxy
@_obj = )" AsClassNameInternal R"((GetActorType());
}
// The copy constructor performs a deep copy
)" AsClassName R"((const )" AsClassName R"( &o)
{
// Create a new C++ instance and copy content
@_obj = )" AsClassNameInternal R"((GetActorType());
_obj = o._obj;
}
// Do a deep a copy of the C++ object
)" AsClassName R"( &opAssign(const )" AsClassName R"( &o)
{
// copy content of C++ instance
_obj = o._obj;
return this;
}
// The script class can be implicitly cast to the C++ type through the opImplCast method
)" AsClassNameInternal R"( @opImplCast() { return _obj; }
// Hold a reference to the C++ side of the proxy
protected )" AsClassNameInternal R"( @_obj;
protected int GetActorType() { return 0; }
// Overridable events
//bool OnActivated(array<uint8> &in eventParams) { return false; }
//bool OnTileDeactivate(int tx1, int ty1, int tx2, int ty2) { return true; }
//void OnHealthChanged() { }
//bool OnPerish() { return true; }
//void OnUpdate(float timeMult) { }
//void OnUpdateHitbox() { }
//void OnHitFloor(float timeMult) { }
//void OnHitCeiling(float timeMult) { }
//void OnHitWall(float timeMult) { }
//void OnAnimationStarted() { }
//void OnAnimationFinished() { }
// Properties
float X { get const { return _obj.X; } }
float Y { get const { return _obj.Y; } }
float SpeedX { get const { return _obj.SpeedX; } set { _obj.SpeedX = value; } }
float SpeedY { get const { return _obj.SpeedY; } set { _obj.SpeedY = value; } }
float ExternalForceX { get const { return _obj.ExternalForceX; } set { _obj.ExternalForceX = value; } }
float ExternalForceY { get const { return _obj.ExternalForceY; } set { _obj.ExternalForceY = value; } }
float Elasticity { get const { return _obj.Elasticity; } set { _obj.Elasticity = value; } }
float Friction { get const { return _obj.Friction; } set { _obj.Friction = value; } }
int Health { get const { return _obj.Health; } set { _obj.Health = value; } }
float Alpha { get const { return _obj.Alpha; } set { _obj.Alpha = value; } }
uint16 Layer { get const { return _obj.Layer; } set { _obj.Layer = value; } }
// Methods
void DecreaseHealth(int amount) { _obj.DecreaseHealth(amount); }
bool MoveTo(float x, float y, bool force = false) { return _obj.MoveTo(x, y, force); }
bool MoveBy(float x, float y, bool force = false) { return _obj.MoveBy(x, y, force); }
void TryStandardMovement(float timeMult) { _obj.TryStandardMovement(timeMult); }
void RequestMetadata(const string &in path) { _obj.RequestMetadata(path); }
void PlaySfx(const string &in identifier, float gain = 1.0, float pitch = 1.0) { _obj.PlaySfx(identifier, gain, pitch); }
void SetAnimation(int state) { _obj.SetAnimation(state); }
}
shared abstract class CollectibleBase : )" AsClassName R"(
{
protected int GetActorType() final { return 1; }
// Overridable events
//bool OnCollect(Player@ player) { return true; }
// Properties
int ScoreValue { get const { return _obj.ScoreValue; } set { _obj.ScoreValue = value; } }
}
)";
int r;
r = engine->RegisterObjectType(AsClassNameInternal, 0, asOBJ_REF); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectBehaviour(AsClassNameInternal, asBEHAVE_FACTORY, AsClassNameInternal " @f(int)", asFUNCTION(ScriptActorWrapper::Factory), asCALL_CDECL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectBehaviour(AsClassNameInternal, asBEHAVE_ADDREF, "void f()", asMETHOD(ScriptActorWrapper, AddRef), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectBehaviour(AsClassNameInternal, asBEHAVE_RELEASE, "void f()", asMETHOD(ScriptActorWrapper, Release), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, AsClassNameInternal " &opAssign(const " AsClassNameInternal " &in)", asMETHOD(ScriptActorWrapper, operator=), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectProperty(AsClassNameInternal, "float X", asOFFSET(ScriptActorWrapper, _pos.X)); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectProperty(AsClassNameInternal, "float Y", asOFFSET(ScriptActorWrapper, _pos.Y)); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectProperty(AsClassNameInternal, "float SpeedX", asOFFSET(ScriptActorWrapper, _speed.X)); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectProperty(AsClassNameInternal, "float SpeedY", asOFFSET(ScriptActorWrapper, _speed.Y)); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectProperty(AsClassNameInternal, "float ExternalForceX", asOFFSET(ScriptActorWrapper, _externalForce.X)); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectProperty(AsClassNameInternal, "float ExternalForceY", asOFFSET(ScriptActorWrapper, _externalForce.Y)); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectProperty(AsClassNameInternal, "float Elasticity", asOFFSET(ScriptActorWrapper, _elasticity)); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectProperty(AsClassNameInternal, "float Friction", asOFFSET(ScriptActorWrapper, _friction)); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectProperty(AsClassNameInternal, "int Health", asOFFSET(ScriptActorWrapper, _health)); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectProperty(AsClassNameInternal, "int ScoreValue", asOFFSET(ScriptActorWrapper, _scoreValue)); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "float get_Alpha() const property", asMETHOD(ScriptActorWrapper, asGetAlpha), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "void set_Alpha(float) property", asMETHOD(ScriptActorWrapper, asSetAlpha), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "uint16 get_Layer() const property", asMETHOD(ScriptActorWrapper, asGetLayer), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "void set_Layer(uint16) property", asMETHOD(ScriptActorWrapper, asSetLayer), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "void DecreaseHealth(int)", asMETHOD(ScriptActorWrapper, asDecreaseHealth), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "bool MoveTo(float, float, bool)", asMETHOD(ScriptActorWrapper, asMoveTo), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "bool MoveBy(float, float, bool)", asMETHOD(ScriptActorWrapper, asMoveBy), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "void TryStandardMovement(float)", asMETHOD(ScriptActorWrapper, asTryStandardMovement), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "void RequestMetadata(const string &in)", asMETHOD(ScriptActorWrapper, asRequestMetadata), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "void PlaySfx(const string &in, float, float)", asMETHOD(ScriptActorWrapper, asPlaySfx), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = engine->RegisterObjectMethod(AsClassNameInternal, "void SetAnimation(int)", asMETHOD(ScriptActorWrapper, asSetAnimationState), asCALL_THISCALL); RETURN_ASSERT(r >= 0);
r = module->AddScriptSection("__" AsClassName, AsLibrary, arraySize(AsLibrary) - 1, 0); RETURN_ASSERT(r >= 0);
}
ScriptActorWrapper* ScriptActorWrapper::Factory(std::int32_t actorType)
{
auto ctx = asGetActiveContext();
auto owner = ScriptLoader::FromActiveContext<LevelScriptLoader>();
// Get the function that is calling the factory, so we can be certain it is the our internal script class
asIScriptFunction* func = ctx->GetFunction(0);
if (func->GetObjectType() == nullptr || StringView(func->GetObjectType()->GetName()) != StringView(AsClassName)) {
ctx->SetException("Cannot manually instantiate " AsClassNameInternal);
return nullptr;
}
asIScriptObject* obj = static_cast<asIScriptObject*>(ctx->GetThisPointer());
switch (actorType) {
default:
case 0: {
void* mem = asAllocMem(sizeof(ScriptActorWrapper));
return new(mem) ScriptActorWrapper(owner, obj);
}
case 1: {
void* mem = asAllocMem(sizeof(ScriptCollectibleWrapper));
return new(mem) ScriptCollectibleWrapper(owner, obj);
}
}
}
void ScriptActorWrapper::AddRef()
{
_refCount++;
// Increment also the reference counter to the script side, so it isn't accidentally destroyed before the C++ side
if (!_isDead->Get()) {
_obj->AddRef();
}
}
void ScriptActorWrapper::Release()
{
// Release the script instance too
if (!_isDead->Get()) {
_obj->Release();
}
if (--_refCount == 0) {
this->~ScriptActorWrapper();
asFreeMem(this);
}
}
Task<bool> ScriptActorWrapper::OnActivatedAsync(const Actors::ActorActivationDetails& details)
{
if (_isDead->Get()) {
async_return false;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptFunction* func = _obj->GetObjectType()->GetMethodByDecl("bool OnActivated(array<uint8> &in)");
if (func == nullptr) {
async_return false;
}
SetState(ActorState::CollideWithOtherActors, _onHandleCollision != nullptr);
CScriptArray* eventParams = CScriptArray::Create(engine->GetTypeInfoByDecl("array<uint8>"), Events::EventSpawner::SpawnParamsSize);
std::memcpy(eventParams->At(0), details.Params, Events::EventSpawner::SpawnParamsSize);
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(func);
ctx->SetObject(_obj);
ctx->SetArgObject(0, eventParams);
std::int32_t r = ctx->Execute();
bool result;
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
result = true;
} else {
result = (ctx->GetReturnByte() != 0);
}
eventParams->Release();
engine->ReturnContext(ctx);
async_return result;
}
bool ScriptActorWrapper::OnTileDeactivated()
{
if (_onTileDeactivated == nullptr || _isDead->Get()) {
return true;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(_onTileDeactivated);
ctx->SetObject(_obj);
std::int32_t r = ctx->Execute();
bool result;
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
result = true;
} else {
result = (ctx->GetReturnByte() != 0);
}
engine->ReturnContext(ctx);
return result;
}
void ScriptActorWrapper::OnHealthChanged(ActorBase* collider)
{
if (_onHealthChanged == nullptr || _isDead->Get()) {
return;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(_onHealthChanged);
ctx->SetObject(_obj);
std::int32_t r = ctx->Execute();
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
}
engine->ReturnContext(ctx);
}
bool ScriptActorWrapper::OnPerish(ActorBase* collider)
{
if (_isDead->Get()) {
return ActorBase::OnPerish(collider);
}
asIScriptFunction* func = _obj->GetObjectType()->GetMethodByDecl("bool OnPerish()");
if (func == nullptr) {
return ActorBase::OnPerish(collider);
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(func);
ctx->SetObject(_obj);
std::int32_t r = ctx->Execute();
bool result;
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
result = true;
} else {
result = (ctx->GetReturnByte() != 0);
}
engine->ReturnContext(ctx);
return (result && ActorBase::OnPerish(collider));
}
void ScriptActorWrapper::OnUpdate(float timeMult)
{
if (_onUpdate == nullptr || _isDead->Get()) {
return;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(_onUpdate);
ctx->SetObject(_obj);
ctx->SetArgFloat(0, timeMult);
std::int32_t r = ctx->Execute();
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
}
engine->ReturnContext(ctx);
}
void ScriptActorWrapper::OnUpdateHitbox()
{
if (_onUpdateHitbox == nullptr || _isDead->Get()) {
// Call base implementation if not overriden
ActorBase::OnUpdateHitbox();
return;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(_onUpdateHitbox);
ctx->SetObject(_obj);
std::int32_t r = ctx->Execute();
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
}
engine->ReturnContext(ctx);
}
bool ScriptActorWrapper::OnHandleCollision(std::shared_ptr<ActorBase> other)
{
if (_onHandleCollision != nullptr) {
if (auto* otherWrapper = runtime_cast<ScriptActorWrapper>(other.get())) {
asIScriptEngine* engine = _obj->GetEngine();
asITypeInfo* typeInfo = _levelScripts->GetMainModule()->GetTypeInfoByName(AsClassName);
if (typeInfo != nullptr) {
asIScriptContext* ctx = engine->RequestContext();
CScriptHandle handle(otherWrapper->_obj, typeInfo);
ctx->Prepare(_onHandleCollision);
ctx->SetObject(_obj);
std::int32_t p = ctx->SetArgObject(0, &handle);
std::int32_t r = ctx->Execute();
bool result;
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
result = true;
} else {
result = (ctx->GetReturnByte() != 0);
}
engine->ReturnContext(ctx);
if (result) {
return true;
}
}
} else if (auto* player = runtime_cast<Player>(other.get())) {
asIScriptEngine* engine = _obj->GetEngine();
asITypeInfo* typeInfo = engine->GetTypeInfoByName("Player");
if (typeInfo != nullptr) {
asIScriptContext* ctx = engine->RequestContext();
void* mem = asAllocMem(sizeof(ScriptPlayerWrapper));
ScriptPlayerWrapper* playerWrapper = new(mem) ScriptPlayerWrapper(_levelScripts, player);
CScriptHandle handle(playerWrapper, typeInfo);
ctx->Prepare(_onHandleCollision);
ctx->SetObject(_obj);
std::int32_t p = ctx->SetArgObject(0, &handle);
std::int32_t r = ctx->Execute();
bool result;
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
result = true;
} else {
result = (ctx->GetReturnByte() != 0);
}
engine->ReturnContext(ctx);
playerWrapper->Release();
if (result) {
return true;
}
}
}
}
return ActorBase::OnHandleCollision(other);
}
bool ScriptActorWrapper::OnDraw(RenderQueue& renderQueue)
{
// TODO
return ActorBase::OnDraw(renderQueue);
}
void ScriptActorWrapper::OnHitFloor(float timeMult)
{
if (_onHitFloor == nullptr || _isDead->Get()) {
return;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(_onHitFloor);
ctx->SetObject(_obj);
ctx->SetArgFloat(0, timeMult);
std::int32_t r = ctx->Execute();
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
}
engine->ReturnContext(ctx);
}
void ScriptActorWrapper::OnHitCeiling(float timeMult)
{
if (_onHitCeiling == nullptr || _isDead->Get()) {
return;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(_onHitCeiling);
ctx->SetObject(_obj);
ctx->SetArgFloat(0, timeMult);
std::int32_t r = ctx->Execute();
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
}
engine->ReturnContext(ctx);
}
void ScriptActorWrapper::OnHitWall(float timeMult)
{
if (_onHitWall == nullptr || _isDead->Get()) {
return;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(_onHitWall);
ctx->SetObject(_obj);
ctx->SetArgFloat(0, timeMult);
std::int32_t r = ctx->Execute();
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
}
engine->ReturnContext(ctx);
}
void ScriptActorWrapper::OnAnimationStarted()
{
if (_onAnimationStarted == nullptr || _isDead->Get()) {
return;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(_onAnimationStarted);
ctx->SetObject(_obj);
std::int32_t r = ctx->Execute();
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
}
engine->ReturnContext(ctx);
}
void ScriptActorWrapper::OnAnimationFinished()
{
// Always call base implementation
ActorBase::OnAnimationFinished();
if (_onAnimationFinished == nullptr || _isDead->Get()) {
return;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
ctx->Prepare(_onAnimationFinished);
ctx->SetObject(_obj);
std::int32_t r = ctx->Execute();
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
}
engine->ReturnContext(ctx);
}
float ScriptActorWrapper::asGetAlpha() const
{
return _renderer.alpha();
}
void ScriptActorWrapper::asSetAlpha(float value)
{
_renderer.setAlphaF(value);
}
uint16_t ScriptActorWrapper::asGetLayer() const
{
return _renderer.layer();
}
void ScriptActorWrapper::asSetLayer(std::uint16_t value)
{
_renderer.setLayer(value);
}
void ScriptActorWrapper::asDecreaseHealth(std::int32_t amount)
{
DecreaseHealth(amount);
}
bool ScriptActorWrapper::asMoveTo(float x, float y, bool force)
{
return MoveInstantly(Vector2f(x, y), (force ? MoveType::Absolute | MoveType::Force : MoveType::Absolute));
}
bool ScriptActorWrapper::asMoveBy(float x, float y, bool force)
{
return MoveInstantly(Vector2f(x, y), (force ? MoveType::Relative | MoveType::Force : MoveType::Relative));
}
void ScriptActorWrapper::asTryStandardMovement(float timeMult)
{
TileCollisionParams params = { TileDestructType::None, _speed.Y >= 0.0f };
TryStandardMovement(timeMult, params);
}
void ScriptActorWrapper::asRequestMetadata(const String& path)
{
RequestMetadata(path);
}
void ScriptActorWrapper::asPlaySfx(const String& identifier, float gain, float pitch)
{
PlaySfx(identifier, gain, pitch);
}
void ScriptActorWrapper::asSetAnimationState(std::int32_t state)
{
SetAnimation((AnimState)state);
}
ScriptCollectibleWrapper::ScriptCollectibleWrapper(LevelScriptLoader* levelScripts, asIScriptObject* obj)
: ScriptActorWrapper(levelScripts, obj), _untouched(true), _phase(0.0f), _timeLeft(0.0f), _startingY(0.0f)
{
_onCollect = _obj->GetObjectType()->GetMethodByDecl("bool OnCollect(Player@)");
}
Task<bool> ScriptCollectibleWrapper::OnActivatedAsync(const ActorActivationDetails& details)
{
_elasticity = 0.6f;
Vector2f pos = _pos;
_phase = ((pos.X / 32) + (pos.Y / 32)) * 2.0f;
if ((GetState() & (ActorState::IsCreatedFromEventMap | ActorState::IsFromGenerator)) != ActorState::None) {
_untouched = true;
SetState(ActorState::ApplyGravitation, false);
_startingY = pos.Y;
} else {
_untouched = false;
SetState(ActorState::ApplyGravitation, true);
_timeLeft = 90.0f * FrameTimer::FramesPerSecond;
}
bool success = async_await ScriptActorWrapper::OnActivatedAsync(details);
SetState(ActorState::CollideWithOtherActors | ActorState::SkipPerPixelCollisions, true);
async_return success;
}
bool ScriptCollectibleWrapper::OnHandleCollision(std::shared_ptr<ActorBase> other)
{
if (auto* player = runtime_cast<Player>(other.get())) {
if (OnCollect(player)) {
return true;
}
} else {
bool shouldDrop = _untouched && (runtime_cast<Weapons::ShotBase>(other.get()) ||
runtime_cast<Weapons::TNT*>(other.get()) || runtime_cast<Enemies::TurtleShell*>(other.get()));
if (shouldDrop) {
Vector2f speed = other->GetSpeed();
_externalForce.X += speed.X / 2.0f * (0.9f + Random().NextFloat(0.0f, 0.2f));
_externalForce.Y += -speed.Y / 4.0f * (0.9f + Random().NextFloat(0.0f, 0.2f));
_untouched = false;
SetState(ActorState::ApplyGravitation, true);
}
}
return ScriptActorWrapper::OnHandleCollision(std::move(other));
}
bool ScriptCollectibleWrapper::OnCollect(Player* player)
{
if (_onCollect == nullptr || _isDead->Get()) {
return false;
}
asIScriptEngine* engine = _obj->GetEngine();
asIScriptContext* ctx = engine->RequestContext();
void* mem = asAllocMem(sizeof(ScriptPlayerWrapper));
ScriptPlayerWrapper* playerWrapper = new(mem) ScriptPlayerWrapper(_levelScripts, player);
ctx->Prepare(_onCollect);
ctx->SetObject(_obj);
ctx->SetArgObject(0, playerWrapper);
std::int32_t r = ctx->Execute();
bool result;
if (r == asEXECUTION_EXCEPTION) {
LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.",
ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration());
result = true;
} else {
result = (ctx->GetReturnByte() != 0);
}
engine->ReturnContext(ctx);
playerWrapper->Release();
if (result) {
player->AddScore(_scoreValue);
Explosion::Create(_levelHandler, Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer()), Explosion::Type::Generator);
DecreaseHealth(INT32_MAX);
return true;
} else {
return false;
}
}
}
#endif
|