summaryrefslogtreecommitdiff
path: root/src/com/replica/replicaisland/DynamicCollisionComponent.java
blob: 05fbb6d2fdf960547bc61d9ad3d3fa3a2f2b0d0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.replica.replicaisland;

/**
 * A component to include dynamic collision volumes (such as those produced every frame from
 * animating sprites) in the dynamic collision world.  Given a set of "attack" volumes and 
 * "vulnerability" volumes (organized such that only attack vs vulnerability intersections result
 * in valid "hits"), this component creates a bounding volume that encompasses the set and submits
 * it to the dynamic collision system.  Including this component in a game object will allow it to
 * send and receive hits to other game objects.
 */
public class DynamicCollisionComponent extends GameComponent {
    private FixedSizeArray<CollisionVolume> mAttackVolumes;
    private FixedSizeArray<CollisionVolume> mVulnerabilityVolumes;
    private SphereCollisionVolume mBoundingVolume;
    private HitReactionComponent mHitReactionComponent;
    
    public DynamicCollisionComponent() {
        super();
        mBoundingVolume = new SphereCollisionVolume(0.0f, 0.0f, 0.0f);
        setPhase(ComponentPhases.FRAME_END.ordinal());
        reset();
    }
    
    @Override
    public void reset() {
       mAttackVolumes = null;
       mVulnerabilityVolumes = null;
       mBoundingVolume.setCenter(Vector2.ZERO);
       mBoundingVolume.setRadius(0.0f);
       mHitReactionComponent = null;     
    }
    
    @Override
    public void update(float timeDelta, BaseObject parent) {
        GameObjectCollisionSystem collision = sSystemRegistry.gameObjectCollisionSystem;
        if (collision != null && mBoundingVolume.getRadius() > 0.0f) {
            collision.registerForCollisions((GameObject)parent, mHitReactionComponent, mBoundingVolume, 
                    mAttackVolumes, mVulnerabilityVolumes);
        }
    }
    
    public void setHitReactionComponent(HitReactionComponent component) {
        mHitReactionComponent = component;
    }
    
    public void setCollisionVolumes(FixedSizeArray<CollisionVolume> attackVolumes, 
            FixedSizeArray<CollisionVolume> vulnerableVolumes) {
        if (mVulnerabilityVolumes != vulnerableVolumes || mAttackVolumes != attackVolumes) {
            mAttackVolumes = attackVolumes;
            mVulnerabilityVolumes = vulnerableVolumes;
            mBoundingVolume.reset();
            if (mAttackVolumes != null) {
               final int count = mAttackVolumes.getCount();
               for (int x = 0; x < count; x++) {
                   mBoundingVolume.growBy(mAttackVolumes.get(x));
               }
            }
            
            if (mVulnerabilityVolumes != null) {
                final int count = mVulnerabilityVolumes.getCount();
                for (int x = 0; x < count; x++) {
                    mBoundingVolume.growBy(mVulnerabilityVolumes.get(x));
                }
             }
        }
    }
}