Make C4Rope a proplist for easy script access

rope
Armin Burgmeier 2012-04-29 00:09:04 +02:00
parent a84faff0cf
commit 89c63b393b
7 changed files with 88 additions and 13 deletions

View File

@ -477,6 +477,7 @@ set(OC_CLONK_SOURCES
src/object/C4ObjectScript.cpp
src/object/C4Rope.cpp
src/object/C4Rope.h
src/object/C4RopeScript.cpp
src/object/C4Sector.cpp
src/object/C4Sector.h
src/object/C4Shape.cpp

View File

@ -6,7 +6,7 @@ func Initialize()
var b = CreateObject(Rock, 200, 100, NO_OWNER);
a->SetCategory(C4D_StaticBack);
b->SetCategory(C4D_StaticBack);
CreateRope2(a, b, 20, LiftTower_Rope);
Scenario.rope = CreateRope2(a, b, 20, LiftTower_Rope);
//CreateRope2(a, b, 1, LiftTower_Rope);
}

View File

@ -2188,6 +2188,7 @@ bool C4Game::InitScriptEngine()
InitCoreFunctionMap(&ScriptEngine);
InitObjectFunctionMap(&ScriptEngine);
InitGameFunctionMap(&ScriptEngine);
Ropes.InitFunctionMap(&ScriptEngine);
// system functions: check if system group is open
if (!Application.OpenSystemGroup())

View File

@ -2125,17 +2125,16 @@ static bool FnSetMeshMaterial(C4AulObjectContext* ctx, C4String* Material, int i
return true;
}
static bool FnCreateRope(C4AulContext *cthr, C4Object* First, C4Object* Second, int iSegments, C4PropList* Graphics)
static C4PropList* FnCreateRope(C4AulContext *cthr, C4Object* First, C4Object* Second, int iSegments, C4PropList* Graphics)
{
try
{
Game.Ropes.CreateRope(First, Second, iSegments, &Graphics->GetDef()->Graphics);
return true;
return Game.Ropes.CreateRope(First, Second, iSegments, &Graphics->GetDef()->Graphics);
}
catch(const C4RopeError& err)
{
DebugLogF("Failed to create rope: %s", err.what());
return false;
return NULL;
}
}

View File

@ -204,9 +204,9 @@ void C4RopeEnd::Execute(C4Real dt)
fx = fy = Fix0;
}
C4Rope::C4Rope(C4Object* first_obj, C4Object* second_obj, int32_t n_segments, C4DefGraphics* graphics):
w(5.0f), Graphics(graphics), n_segments(n_segments), l(ObjectDistance(first_obj, second_obj) / (n_segments + 1)),
k(Fix1*3), eta(Fix1*3), n_iterations(20)
C4Rope::C4Rope(C4PropList* Prototype, C4Object* first_obj, C4Object* second_obj, int32_t n_segments, C4DefGraphics* graphics):
C4PropListNumbered(Prototype), w(5.0f), Graphics(graphics), n_segments(n_segments),
l(ObjectDistance(first_obj, second_obj) / (n_segments + 1)), k(Fix1*3), eta(Fix1*3), n_iterations(20)
{
if(!PathFree(first_obj->GetX(), first_obj->GetY(), second_obj->GetX(), second_obj->GetY()))
throw C4RopeError("Path between objects is blocked");
@ -272,8 +272,8 @@ void C4Rope::Solve(TRopeType1* prev, TRopeType2* next) //C4RopeSegment* prev, C4
// Could add air/water friction here
// Apply forces to masses. Don't apply gravity to objects since it's applied already in C4Object execution.
prev->AddForce(-fx, -fy + (GetObject(prev) ? Fix0 : prev->GetMass() * ::Landscape.Gravity/5));
next->AddForce(fx, fy + (GetObject(next) ? Fix0 : next->GetMass() * ::Landscape.Gravity/5));
prev->AddForce(-fx, -fy + (::GetObject(prev) ? Fix0 : prev->GetMass() * ::Landscape.Gravity/5));
next->AddForce(fx, fy + (::GetObject(next) ? Fix0 : next->GetMass() * ::Landscape.Gravity/5));
}
void C4Rope::Execute()
@ -399,7 +399,7 @@ C4RopeList::C4RopeList()
C4Rope* C4RopeList::CreateRope(C4Object* first_obj, C4Object* second_obj, int32_t n_segments, C4DefGraphics* graphics)
{
Ropes.push_back(new C4Rope(first_obj, second_obj, n_segments, graphics));
Ropes.push_back(new C4Rope(RopeAul.GetPropList(), first_obj, second_obj, n_segments, graphics));
return Ropes.back();
}

View File

@ -89,15 +89,18 @@ private:
};
};
class C4Rope
class C4Rope: public C4PropListNumbered
{
public:
C4Rope(C4Object* first_obj, C4Object* second_obj, int32_t n_segments, C4DefGraphics* graphics);
C4Rope(C4PropList* Prototype, C4Object* first_obj, C4Object* second_obj, int32_t n_segments, C4DefGraphics* graphics);
~C4Rope();
void Draw(C4TargetFacet& cgo, C4BltTransform* pTransform);
void Execute();
C4Object* GetFront() const { return front->GetObject(); }
C4Object* GetBack() const { return back->GetObject(); }
private:
template<typename TRopeType1, typename TRopeType2>
void Solve(TRopeType1* prev, TRopeType2* next);
@ -122,10 +125,27 @@ private:
unsigned int n_iterations;
};
class C4RopeAul: public C4AulScript
{
public:
C4RopeAul();
virtual ~C4RopeAul();
virtual bool Delete() { return false; }
virtual C4PropList* GetPropList() { return RopeDef; }
void InitFunctionMap(C4AulScriptEngine* pEngine);
protected:
C4PropList* RopeDef;
};
class C4RopeList
{
public:
C4RopeList();
void InitFunctionMap(C4AulScriptEngine* pEngine) { RopeAul.InitFunctionMap(pEngine); }
void Execute();
void Draw(C4TargetFacet& cgo, C4BltTransform* pTransform);
@ -133,6 +153,7 @@ public:
C4Rope* CreateRope(C4Object* first_obj, C4Object* second_obj, int32_t n_segments, C4DefGraphics* graphics);
private:
C4RopeAul RopeAul;
std::vector<C4Rope*> Ropes;
};

View File

@ -0,0 +1,53 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2012 Armin Burgmeier
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
#include <C4Include.h>
#include <C4Rope.h>
#include <C4AulDefFunc.h>
static C4Object* FnGetFront(C4AulContext* Context)
{
return static_cast<C4Rope*>(Context->Def)->GetFront();
}
static C4Object* FnGetBack(C4AulContext* Context)
{
return static_cast<C4Rope*>(Context->Def)->GetBack();
}
C4RopeAul::C4RopeAul():
RopeDef(NULL)
{
}
C4RopeAul::~C4RopeAul()
{
delete RopeDef;
}
void C4RopeAul::InitFunctionMap(C4AulScriptEngine* pEngine)
{
delete RopeDef;
RopeDef = C4PropList::NewScen();
RopeDef->SetName("C4Rope");
Reg2List(pEngine);
::AddFunc(this, "GetFront", FnGetFront);
::AddFunc(this, "GetBack", FnGetBack);
RopeDef->Freeze();
}