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
17
18package com.replica.replicaisland;
19
20/**
21 * A component to include dynamic collision volumes (such as those produced every frame from
22 * animating sprites) in the dynamic collision world.  Given a set of "attack" volumes and
23 * "vulnerability" volumes (organized such that only attack vs vulnerability intersections result
24 * in valid "hits"), this component creates a bounding volume that encompasses the set and submits
25 * it to the dynamic collision system.  Including this component in a game object will allow it to
26 * send and receive hits to other game objects.
27 */
28public class DynamicCollisionComponent extends GameComponent {
29    private FixedSizeArray<CollisionVolume> mAttackVolumes;
30    private FixedSizeArray<CollisionVolume> mVulnerabilityVolumes;
31    private SphereCollisionVolume mBoundingVolume;
32    private HitReactionComponent mHitReactionComponent;
33
34    public DynamicCollisionComponent() {
35        super();
36        mBoundingVolume = new SphereCollisionVolume(0.0f, 0.0f, 0.0f);
37        setPhase(ComponentPhases.FRAME_END.ordinal());
38        reset();
39    }
40
41    @Override
42    public void reset() {
43       mAttackVolumes = null;
44       mVulnerabilityVolumes = null;
45       mBoundingVolume.setCenter(Vector2.ZERO);
46       mBoundingVolume.setRadius(0.0f);
47       mHitReactionComponent = null;
48    }
49
50    @Override
51    public void update(float timeDelta, BaseObject parent) {
52        GameObjectCollisionSystem collision = sSystemRegistry.gameObjectCollisionSystem;
53        if (collision != null && mBoundingVolume.getRadius() > 0.0f) {
54            collision.registerForCollisions((GameObject)parent, mHitReactionComponent, mBoundingVolume,
55                    mAttackVolumes, mVulnerabilityVolumes);
56        }
57    }
58
59    public void setHitReactionComponent(HitReactionComponent component) {
60        mHitReactionComponent = component;
61    }
62
63    public void setCollisionVolumes(FixedSizeArray<CollisionVolume> attackVolumes,
64            FixedSizeArray<CollisionVolume> vulnerableVolumes) {
65        if (mVulnerabilityVolumes != vulnerableVolumes || mAttackVolumes != attackVolumes) {
66            mAttackVolumes = attackVolumes;
67            mVulnerabilityVolumes = vulnerableVolumes;
68            mBoundingVolume.reset();
69            if (mAttackVolumes != null) {
70               final int count = mAttackVolumes.getCount();
71               for (int x = 0; x < count; x++) {
72                   mBoundingVolume.growBy(mAttackVolumes.get(x));
73               }
74            }
75
76            if (mVulnerabilityVolumes != null) {
77                final int count = mVulnerabilityVolumes.getCount();
78                for (int x = 0; x < count; x++) {
79                    mBoundingVolume.growBy(mVulnerabilityVolumes.get(x));
80                }
81             }
82        }
83    }
84}
85