summaryrefslogtreecommitdiff
path: root/src/com/replica/replicaisland/PopOutComponent.java
blob: 2f5fda1e0befc551e4999e2e69945edb72527945 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * 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 that implements the "pop-out" AI behavior.  Pop-out characters alternate between 
 * hiding and appearing based on their distance from the player.  They do not move or normally
 * attack.
 */
public class PopOutComponent extends GameComponent {
    private static final int DEFAULT_APPEAR_DISTANCE = 120;
    private static final int DEFAULT_HIDE_DISTANCE = 190;
    private static final int DEFAULT_ATTACK_DISTANCE = 0;   // No attacking by default.
    private float mAppearDistance;
    private float mHideDistance;
    private float mAttackDistance;
    private float mAttackDelay;
    private float mAttackLength;
    private float mAttackStartTime;
    private Vector2 mDistance;
    private int mState;
    private float mLastAttackCompletedTime;
    
    private final static int STATE_HIDDEN = 0;
    private final static int STATE_VISIBLE = 1;
    private final static int STATE_ATTACKING = 2;
    
    public PopOutComponent() {
        super();
        setPhase(GameComponent.ComponentPhases.THINK.ordinal());
        mDistance = new Vector2();
        reset();
    }
    
    @Override
    public void reset() {
        mAttackDelay = 0;
        mAttackLength = 0;
        mAttackDistance = DEFAULT_ATTACK_DISTANCE;
        mAppearDistance = DEFAULT_APPEAR_DISTANCE;
        mHideDistance = DEFAULT_HIDE_DISTANCE;
        mState = STATE_HIDDEN;
        mLastAttackCompletedTime = 0.0f;
    }
    
    @Override
    public void update(float timeDelta, BaseObject parent) {   
        GameObject parentObject = (GameObject) parent;

        GameObjectManager manager = sSystemRegistry.gameObjectManager;
        if (manager != null) {
            GameObject player = manager.getPlayer();
            if (player != null) {
                mDistance.set(player.getPosition());
                mDistance.subtract(parentObject.getPosition());
                
                TimeSystem time = sSystemRegistry.timeSystem;
                final float currentTime = time.getGameTime();
                
                switch(mState) {
                    case STATE_HIDDEN:
                        parentObject.setCurrentAction(GameObject.ActionType.HIDE);
                        if (mDistance.length2() < (mAppearDistance * mAppearDistance)) {
                            mState = STATE_VISIBLE;
                            mLastAttackCompletedTime = currentTime;
                        }
                        break;
                    case STATE_VISIBLE:
                        parentObject.setCurrentAction(GameObject.ActionType.IDLE);
                        if (mDistance.length2() > (mHideDistance * mHideDistance)) {
                            mState = STATE_HIDDEN;
                        } else if (mDistance.length2() < (mAttackDistance * mAttackDistance) 
                                && currentTime > mLastAttackCompletedTime + mAttackDelay) {
                            mAttackStartTime = currentTime;
                            mState = STATE_ATTACKING;
                        }
                        break;
                    case STATE_ATTACKING:
                        parentObject.setCurrentAction(GameObject.ActionType.ATTACK);
                        if (currentTime > mAttackStartTime + mAttackLength) {
                            mState = STATE_VISIBLE;
                            mLastAttackCompletedTime = currentTime;
                        }
                        break;
                    default:
                        assert false;
                        break;
                }
                
            }
        }
       
    }

    public void setupAttack(float distance, float delay, float duration) {
        mAttackDistance = distance;
        mAttackDelay = delay;
        mAttackLength = duration;
    }
    
    public void setAppearDistance(float appearDistance) {
        mAppearDistance = appearDistance;
    }

    public void setHideDistance(float hideDistance) {
        mHideDistance = hideDistance;
    }
    
    
}