1cfd74d65d832137e20e193c960802afba73b5d38sm/*
23c1e67e433728684b5f228c5d4f3e5b1457bb271sm * Copyright (C) 2010 The Android Open Source Project
3cfd74d65d832137e20e193c960802afba73b5d38sm *
4cfd74d65d832137e20e193c960802afba73b5d38sm * Licensed under the Apache License, Version 2.0 (the "License");
5cfd74d65d832137e20e193c960802afba73b5d38sm * you may not use this file except in compliance with the License.
6cfd74d65d832137e20e193c960802afba73b5d38sm * You may obtain a copy of the License at
7cfd74d65d832137e20e193c960802afba73b5d38sm *
8cfd74d65d832137e20e193c960802afba73b5d38sm *      http://www.apache.org/licenses/LICENSE-2.0
9cfd74d65d832137e20e193c960802afba73b5d38sm *
10cfd74d65d832137e20e193c960802afba73b5d38sm * Unless required by applicable law or agreed to in writing, software
11cfd74d65d832137e20e193c960802afba73b5d38sm * distributed under the License is distributed on an "AS IS" BASIS,
12cfd74d65d832137e20e193c960802afba73b5d38sm * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cfd74d65d832137e20e193c960802afba73b5d38sm * See the License for the specific language governing permissions and
14cfd74d65d832137e20e193c960802afba73b5d38sm * limitations under the License.
15cfd74d65d832137e20e193c960802afba73b5d38sm */
16cfd74d65d832137e20e193c960802afba73b5d38sm
17cfd74d65d832137e20e193c960802afba73b5d38sm
18cfd74d65d832137e20e193c960802afba73b5d38smpackage com.replica.replicaisland;
19cfd74d65d832137e20e193c960802afba73b5d38sm
20cfd74d65d832137e20e193c960802afba73b5d38sm/**
21cfd74d65d832137e20e193c960802afba73b5d38sm * A component to include dynamic collision volumes (such as those produced every frame from
22cfd74d65d832137e20e193c960802afba73b5d38sm * animating sprites) in the dynamic collision world.  Given a set of "attack" volumes and
23cfd74d65d832137e20e193c960802afba73b5d38sm * "vulnerability" volumes (organized such that only attack vs vulnerability intersections result
24cfd74d65d832137e20e193c960802afba73b5d38sm * in valid "hits"), this component creates a bounding volume that encompasses the set and submits
25cfd74d65d832137e20e193c960802afba73b5d38sm * it to the dynamic collision system.  Including this component in a game object will allow it to
26cfd74d65d832137e20e193c960802afba73b5d38sm * send and receive hits to other game objects.
27cfd74d65d832137e20e193c960802afba73b5d38sm */
28cfd74d65d832137e20e193c960802afba73b5d38smpublic class DynamicCollisionComponent extends GameComponent {
29cfd74d65d832137e20e193c960802afba73b5d38sm    private FixedSizeArray<CollisionVolume> mAttackVolumes;
30cfd74d65d832137e20e193c960802afba73b5d38sm    private FixedSizeArray<CollisionVolume> mVulnerabilityVolumes;
31cfd74d65d832137e20e193c960802afba73b5d38sm    private SphereCollisionVolume mBoundingVolume;
32cfd74d65d832137e20e193c960802afba73b5d38sm    private HitReactionComponent mHitReactionComponent;
33cfd74d65d832137e20e193c960802afba73b5d38sm
34cfd74d65d832137e20e193c960802afba73b5d38sm    public DynamicCollisionComponent() {
35cfd74d65d832137e20e193c960802afba73b5d38sm        super();
36cfd74d65d832137e20e193c960802afba73b5d38sm        mBoundingVolume = new SphereCollisionVolume(0.0f, 0.0f, 0.0f);
37cfd74d65d832137e20e193c960802afba73b5d38sm        setPhase(ComponentPhases.FRAME_END.ordinal());
38cfd74d65d832137e20e193c960802afba73b5d38sm        reset();
39cfd74d65d832137e20e193c960802afba73b5d38sm    }
40cfd74d65d832137e20e193c960802afba73b5d38sm
41cfd74d65d832137e20e193c960802afba73b5d38sm    @Override
42cfd74d65d832137e20e193c960802afba73b5d38sm    public void reset() {
43cfd74d65d832137e20e193c960802afba73b5d38sm       mAttackVolumes = null;
44cfd74d65d832137e20e193c960802afba73b5d38sm       mVulnerabilityVolumes = null;
45cfd74d65d832137e20e193c960802afba73b5d38sm       mBoundingVolume.setCenter(Vector2.ZERO);
46cfd74d65d832137e20e193c960802afba73b5d38sm       mBoundingVolume.setRadius(0.0f);
47cfd74d65d832137e20e193c960802afba73b5d38sm       mHitReactionComponent = null;
48cfd74d65d832137e20e193c960802afba73b5d38sm    }
49cfd74d65d832137e20e193c960802afba73b5d38sm
50cfd74d65d832137e20e193c960802afba73b5d38sm    @Override
51cfd74d65d832137e20e193c960802afba73b5d38sm    public void update(float timeDelta, BaseObject parent) {
52cfd74d65d832137e20e193c960802afba73b5d38sm        GameObjectCollisionSystem collision = sSystemRegistry.gameObjectCollisionSystem;
53cfd74d65d832137e20e193c960802afba73b5d38sm        if (collision != null && mBoundingVolume.getRadius() > 0.0f) {
54cfd74d65d832137e20e193c960802afba73b5d38sm            collision.registerForCollisions((GameObject)parent, mHitReactionComponent, mBoundingVolume,
55cfd74d65d832137e20e193c960802afba73b5d38sm                    mAttackVolumes, mVulnerabilityVolumes);
56cfd74d65d832137e20e193c960802afba73b5d38sm        }
57cfd74d65d832137e20e193c960802afba73b5d38sm    }
58cfd74d65d832137e20e193c960802afba73b5d38sm
59cfd74d65d832137e20e193c960802afba73b5d38sm    public void setHitReactionComponent(HitReactionComponent component) {
60cfd74d65d832137e20e193c960802afba73b5d38sm        mHitReactionComponent = component;
61cfd74d65d832137e20e193c960802afba73b5d38sm    }
62cfd74d65d832137e20e193c960802afba73b5d38sm
63cfd74d65d832137e20e193c960802afba73b5d38sm    public void setCollisionVolumes(FixedSizeArray<CollisionVolume> attackVolumes,
64cfd74d65d832137e20e193c960802afba73b5d38sm            FixedSizeArray<CollisionVolume> vulnerableVolumes) {
65cfd74d65d832137e20e193c960802afba73b5d38sm        if (mVulnerabilityVolumes != vulnerableVolumes || mAttackVolumes != attackVolumes) {
66cfd74d65d832137e20e193c960802afba73b5d38sm            mAttackVolumes = attackVolumes;
67cfd74d65d832137e20e193c960802afba73b5d38sm            mVulnerabilityVolumes = vulnerableVolumes;
68cfd74d65d832137e20e193c960802afba73b5d38sm            mBoundingVolume.reset();
69cfd74d65d832137e20e193c960802afba73b5d38sm            if (mAttackVolumes != null) {
70cfd74d65d832137e20e193c960802afba73b5d38sm               final int count = mAttackVolumes.getCount();
71cfd74d65d832137e20e193c960802afba73b5d38sm               for (int x = 0; x < count; x++) {
72cfd74d65d832137e20e193c960802afba73b5d38sm                   mBoundingVolume.growBy(mAttackVolumes.get(x));
73cfd74d65d832137e20e193c960802afba73b5d38sm               }
74cfd74d65d832137e20e193c960802afba73b5d38sm            }
75cfd74d65d832137e20e193c960802afba73b5d38sm
76cfd74d65d832137e20e193c960802afba73b5d38sm            if (mVulnerabilityVolumes != null) {
77cfd74d65d832137e20e193c960802afba73b5d38sm                final int count = mVulnerabilityVolumes.getCount();
78cfd74d65d832137e20e193c960802afba73b5d38sm                for (int x = 0; x < count; x++) {
79cfd74d65d832137e20e193c960802afba73b5d38sm                    mBoundingVolume.growBy(mVulnerabilityVolumes.get(x));
80cfd74d65d832137e20e193c960802afba73b5d38sm                }
81cfd74d65d832137e20e193c960802afba73b5d38sm             }
82cfd74d65d832137e20e193c960802afba73b5d38sm        }
83cfd74d65d832137e20e193c960802afba73b5d38sm    }
84cfd74d65d832137e20e193c960802afba73b5d38sm}
85