1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #include "../Precompiled.h"
24 
25 #include "../Core/Context.h"
26 #include "../IO/MemoryBuffer.h"
27 #include "../IO/VectorBuffer.h"
28 #include "../Urho2D/CollisionChain2D.h"
29 #include "../Urho2D/PhysicsUtils2D.h"
30 
31 #include "../DebugNew.h"
32 
33 namespace Urho3D
34 {
35 
36 extern const char* URHO2D_CATEGORY;
37 
CollisionChain2D(Context * context)38 CollisionChain2D::CollisionChain2D(Context* context) :
39     CollisionShape2D(context),
40     loop_(false)
41 {
42     fixtureDef_.shape = &chainShape_;
43 }
44 
~CollisionChain2D()45 CollisionChain2D::~CollisionChain2D()
46 {
47 }
48 
RegisterObject(Context * context)49 void CollisionChain2D::RegisterObject(Context* context)
50 {
51     context->RegisterFactory<CollisionChain2D>(URHO2D_CATEGORY);
52 
53     URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
54     URHO3D_ACCESSOR_ATTRIBUTE("Loop", GetLoop, SetLoop, bool, false, AM_DEFAULT);
55     URHO3D_COPY_BASE_ATTRIBUTES(CollisionShape2D);
56     URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Vertices", GetVerticesAttr, SetVerticesAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_FILE);
57 }
58 
SetLoop(bool loop)59 void CollisionChain2D::SetLoop(bool loop)
60 {
61     if (loop == loop_)
62         return;
63 
64     loop_ = loop;
65 
66     MarkNetworkUpdate();
67     RecreateFixture();
68 }
69 
SetVertexCount(unsigned count)70 void CollisionChain2D::SetVertexCount(unsigned count)
71 {
72     vertices_.Resize(count);
73 }
74 
SetVertex(unsigned index,const Vector2 & vertex)75 void CollisionChain2D::SetVertex(unsigned index, const Vector2& vertex)
76 {
77     if (index >= vertices_.Size())
78         return;
79 
80     vertices_[index] = vertex;
81 
82     if (index == vertices_.Size() - 1)
83     {
84         MarkNetworkUpdate();
85         RecreateFixture();
86     }
87 }
88 
SetVertices(const PODVector<Vector2> & vertices)89 void CollisionChain2D::SetVertices(const PODVector<Vector2>& vertices)
90 {
91     vertices_ = vertices;
92 
93     MarkNetworkUpdate();
94     RecreateFixture();
95 }
96 
SetVerticesAttr(const PODVector<unsigned char> & value)97 void CollisionChain2D::SetVerticesAttr(const PODVector<unsigned char>& value)
98 {
99     if (value.Empty())
100         return;
101 
102     PODVector<Vector2> vertices;
103 
104     MemoryBuffer buffer(value);
105     while (!buffer.IsEof())
106         vertices.Push(buffer.ReadVector2());
107 
108     SetVertices(vertices);
109 }
110 
GetVerticesAttr() const111 PODVector<unsigned char> CollisionChain2D::GetVerticesAttr() const
112 {
113     VectorBuffer ret;
114 
115     for (unsigned i = 0; i < vertices_.Size(); ++i)
116         ret.WriteVector2(vertices_[i]);
117 
118     return ret.GetBuffer();
119 }
120 
ApplyNodeWorldScale()121 void CollisionChain2D::ApplyNodeWorldScale()
122 {
123     RecreateFixture();
124 }
125 
RecreateFixture()126 void CollisionChain2D::RecreateFixture()
127 {
128     ReleaseFixture();
129 
130     PODVector<b2Vec2> b2Vertices;
131     unsigned count = vertices_.Size();
132     b2Vertices.Resize(count);
133 
134     Vector2 worldScale(cachedWorldScale_.x_, cachedWorldScale_.y_);
135     for (unsigned i = 0; i < count; ++i)
136         b2Vertices[i] = ToB2Vec2(vertices_[i] * worldScale);
137 
138 	chainShape_.Clear();
139     if (loop_)
140         chainShape_.CreateLoop(&b2Vertices[0], count);
141     else
142         chainShape_.CreateChain(&b2Vertices[0], count);
143 
144     CreateFixture();
145 }
146 
147 }
148