summaryrefslogtreecommitdiff
path: root/src/com/replica/replicaisland/AttackAtDistanceComponent.java
blob: de52c455898a25522ebc7b56899ce1bd025b4c3e (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
/*
 * 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;

public class AttackAtDistanceComponent extends GameComponent {
    private static final int DEFAULT_ATTACK_DISTANCE = 100;
    private float mAttackDistance;
    private float mAttackDelay;
    private float mAttackLength;
    private float mAttackStartTime;
    private boolean mRequireFacing;
    private Vector2 mDistance;
    
    public AttackAtDistanceComponent() {
        super();
        setPhase(GameComponent.ComponentPhases.THINK.ordinal());
        mDistance = new Vector2();
        reset();
    }
    
    @Override
    public void reset() {
        mAttackDelay = 0;
        mAttackLength = 0;
        mAttackDistance = DEFAULT_ATTACK_DISTANCE;
        mRequireFacing = false;
    }
    
    @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();
                final boolean facingPlayer = 
                    (Utils.sign(player.getPosition().x - parentObject.getPosition().x)
                        == Utils.sign(parentObject.facingDirection.x));
                final boolean facingDirectionCorrect = (mRequireFacing && facingPlayer)
                    || !mRequireFacing;
                if (parentObject.getCurrentAction() == GameObject.ActionType.ATTACK) {
                    if (currentTime > mAttackStartTime + mAttackLength) {
                        parentObject.setCurrentAction(GameObject.ActionType.IDLE);
                    }
                } else if (mDistance.length2() < (mAttackDistance * mAttackDistance) 
                            && currentTime > mAttackStartTime + mAttackLength + mAttackDelay
                            && facingDirectionCorrect) {
                    mAttackStartTime = currentTime;
                    parentObject.setCurrentAction(GameObject.ActionType.ATTACK); 
                } else {
                    parentObject.setCurrentAction(GameObject.ActionType.IDLE);
                }
            } 
        }
       
    }

    public void setupAttack(float distance, float delay, float duration, boolean requireFacing) {
        mAttackDistance = distance;
        mAttackDelay = delay;
        mAttackLength = duration;
        mRequireFacing = requireFacing;
    }
    
  
}