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
20public class AttackAtDistanceComponent extends GameComponent {
21    private static final int DEFAULT_ATTACK_DISTANCE = 100;
22    private float mAttackDistance;
23    private float mAttackDelay;
24    private float mAttackLength;
25    private float mAttackStartTime;
26    private boolean mRequireFacing;
27    private Vector2 mDistance;
28
29    public AttackAtDistanceComponent() {
30        super();
31        setPhase(GameComponent.ComponentPhases.THINK.ordinal());
32        mDistance = new Vector2();
33        reset();
34    }
35
36    @Override
37    public void reset() {
38        mAttackDelay = 0;
39        mAttackLength = 0;
40        mAttackDistance = DEFAULT_ATTACK_DISTANCE;
41        mRequireFacing = false;
42    }
43
44    @Override
45    public void update(float timeDelta, BaseObject parent) {
46        GameObject parentObject = (GameObject) parent;
47
48        GameObjectManager manager = sSystemRegistry.gameObjectManager;
49        if (manager != null) {
50            GameObject player = manager.getPlayer();
51            if (player != null) {
52                mDistance.set(player.getPosition());
53                mDistance.subtract(parentObject.getPosition());
54
55                TimeSystem time = sSystemRegistry.timeSystem;
56                final float currentTime = time.getGameTime();
57                final boolean facingPlayer =
58                    (Utils.sign(player.getPosition().x - parentObject.getPosition().x)
59                        == Utils.sign(parentObject.facingDirection.x));
60                final boolean facingDirectionCorrect = (mRequireFacing && facingPlayer)
61                    || !mRequireFacing;
62                if (parentObject.getCurrentAction() == GameObject.ActionType.ATTACK) {
63                    if (currentTime > mAttackStartTime + mAttackLength) {
64                        parentObject.setCurrentAction(GameObject.ActionType.IDLE);
65                    }
66                } else if (mDistance.length2() < (mAttackDistance * mAttackDistance)
67                            && currentTime > mAttackStartTime + mAttackLength + mAttackDelay
68                            && facingDirectionCorrect) {
69                    mAttackStartTime = currentTime;
70                    parentObject.setCurrentAction(GameObject.ActionType.ATTACK);
71                } else {
72                    parentObject.setCurrentAction(GameObject.ActionType.IDLE);
73                }
74            }
75        }
76
77    }
78
79    public void setupAttack(float distance, float delay, float duration, boolean requireFacing) {
80        mAttackDistance = distance;
81        mAttackDelay = delay;
82        mAttackLength = duration;
83        mRequireFacing = requireFacing;
84    }
85
86
87}
88