1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.replica.replicaisland;
18
19/**
20 * A component that allows a game object to act like a solid object by submitting surfaces to the
21 * background collision system every frame.
22 */
23public class SolidSurfaceComponent extends GameComponent {
24    private FixedSizeArray<Vector2> mStartPoints;
25    private FixedSizeArray<Vector2> mEndPoints;
26    private FixedSizeArray<Vector2> mNormals;
27    private Vector2 mStart;
28    private Vector2 mEnd;
29    private Vector2 mNormal;
30
31    public SolidSurfaceComponent(int maxSurfaceCount) {
32        super();
33
34        inititalize(maxSurfaceCount);
35
36        mStart = new Vector2();
37        mEnd = new Vector2();
38        mNormal = new Vector2();
39
40        setPhase(ComponentPhases.POST_COLLISION.ordinal());
41        reset();
42    }
43
44    @Override
45    public void reset() {
46        mStartPoints.clear();
47        mEndPoints.clear();
48        mNormals.clear();
49    }
50
51    public SolidSurfaceComponent() {
52        super();
53
54        mStart = new Vector2();
55        mEnd = new Vector2();
56        mNormal = new Vector2();
57
58        setPhase(ComponentPhases.POST_COLLISION.ordinal());
59    }
60
61    public void inititalize(int maxSurfaceCount) {
62        if (mStartPoints == null
63                || (mStartPoints != null && mStartPoints.getCount() != maxSurfaceCount)) {
64            mStartPoints = new FixedSizeArray<Vector2>(maxSurfaceCount);
65            mEndPoints = new FixedSizeArray<Vector2>(maxSurfaceCount);
66            mNormals = new FixedSizeArray<Vector2>(maxSurfaceCount);
67        }
68
69        mStartPoints.clear();
70        mEndPoints.clear();
71        mNormals.clear();
72    }
73
74    // Note that this function keeps direct references to the arguments it is passed.
75    public void addSurface(Vector2 startPoint, Vector2 endPoint, Vector2 normal) {
76        mStartPoints.add(startPoint);
77        mEndPoints.add(endPoint);
78        mNormals.add(normal);
79    }
80
81    @Override
82    public void update(float timeDelta, BaseObject parent) {
83        CollisionSystem collision = sSystemRegistry.collisionSystem;
84
85        final FixedSizeArray<Vector2> startPoints = mStartPoints;
86        final FixedSizeArray<Vector2> endPoints = mEndPoints;
87        final FixedSizeArray<Vector2> normals = mNormals;
88
89        final int surfaceCount = startPoints.getCount();
90        if (collision != null && surfaceCount > 0) {
91            GameObject parentObject = (GameObject)parent;
92            final Vector2 position = parentObject.getPosition();
93            Vector2 start = mStart;
94            Vector2 end = mEnd;
95            Vector2 normal = mNormal;
96
97            for (int x = 0; x < surfaceCount; x++) {
98               start.set(startPoints.get(x));
99               if (parentObject.facingDirection.x < 0.0f) {
100                   start.flipHorizontal(parentObject.width);
101               }
102
103               if (parentObject.facingDirection.y < 0.0f) {
104                   start.flipVertical(parentObject.height);
105               }
106               start.add(position);
107
108               end.set(endPoints.get(x));
109               if (parentObject.facingDirection.x < 0.0f) {
110                   end.flipHorizontal(parentObject.width);
111               }
112
113               if (parentObject.facingDirection.y < 0.0f) {
114                   end.flipVertical(parentObject.height);
115               }
116               end.add(position);
117
118               normal.set(normals.get(x));
119               if (parentObject.facingDirection.x < 0.0f) {
120                   normal.flipHorizontal(0);
121               }
122
123               if (parentObject.facingDirection.y < 0.0f) {
124                   normal.flipVertical(0);
125               }
126
127               collision.addTemporarySurface(start, end, normal, parentObject);
128            }
129
130
131        }
132    }
133
134}
135