| 665 | } |
| 666 | |
| 667 | bool ScriptCollectibleWrapper::OnCollect(Player* player) |
| 668 | { |
| 669 | if (_onCollect == nullptr || _isDead->Get()) { |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | asIScriptEngine* engine = _obj->GetEngine(); |
| 674 | asIScriptContext* ctx = engine->RequestContext(); |
| 675 | |
| 676 | void* mem = asAllocMem(sizeof(ScriptPlayerWrapper)); |
| 677 | ScriptPlayerWrapper* playerWrapper = new(mem) ScriptPlayerWrapper(_levelScripts, player); |
| 678 | |
| 679 | ctx->Prepare(_onCollect); |
| 680 | ctx->SetObject(_obj); |
| 681 | ctx->SetArgObject(0, playerWrapper); |
| 682 | std::int32_t r = ctx->Execute(); |
| 683 | bool result; |
| 684 | if (r == asEXECUTION_EXCEPTION) { |
| 685 | LOGE("An exception \"{}\" occurred in \"{}\". Please correct the code and try again.", |
| 686 | ctx->GetExceptionString(), ctx->GetExceptionFunction()->GetDeclaration()); |
| 687 | result = true; |
| 688 | } else { |
| 689 | result = (ctx->GetReturnByte() != 0); |
| 690 | } |
| 691 | |
| 692 | engine->ReturnContext(ctx); |
| 693 | |
| 694 | playerWrapper->Release(); |
| 695 | |
| 696 | if (result) { |
| 697 | player->AddScore(_scoreValue); |
| 698 | Explosion::Create(_levelHandler, Vector3i((std::int32_t)_pos.X, (std::int32_t)_pos.Y, _renderer.layer()), Explosion::Type::Generator); |
| 699 | DecreaseHealth(INT32_MAX); |
| 700 | return true; |
| 701 | } else { |
| 702 | return false; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | #endif |