• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package com.replica.replicaisland;
19 
20 public class HitPlayerComponent extends GameComponent {
21 	float mDistance2;
22 	Vector2 mPlayerPosition;
23 	Vector2 mMyPosition;
24 	HitReactionComponent mHitReact;
25 	int mHitType;
26 	boolean mHitDirection;
27 
HitPlayerComponent()28 	public HitPlayerComponent() {
29         super();
30         mPlayerPosition = new Vector2();
31         mMyPosition = new Vector2();
32         reset();
33         setPhase(ComponentPhases.THINK.ordinal());
34     }
35 
36     @Override
reset()37     public void reset() {
38     	mDistance2 = 0.0f;
39     	mPlayerPosition.zero();
40     	mMyPosition.zero();
41     	mHitReact = null;
42     	mHitType = CollisionParameters.HitType.INVALID;
43     	mHitDirection = false; // by default, hit myself
44     }
45 
46     @Override
update(float timeDelta, BaseObject parent)47     public void update(float timeDelta, BaseObject parent) {
48         GameObjectManager manager = sSystemRegistry.gameObjectManager;
49         if (manager != null && mHitReact != null) {
50         	GameObject player = manager.getPlayer();
51         	if (player != null && player.life > 0) {
52         		mPlayerPosition.set(player.getCenteredPositionX(), player.getCenteredPositionY());
53         		GameObject parentObject = (GameObject)parent;
54         		mMyPosition.set(parentObject.getCenteredPositionX(), parentObject.getCenteredPositionY());
55         		if (mMyPosition.distance2(mPlayerPosition) <= mDistance2) {
56         			HitReactionComponent playerHitReact = player.findByClass(HitReactionComponent.class);
57         			if (playerHitReact != null) {
58         				if (!mHitDirection) {
59         					// hit myself
60         					boolean accepted = mHitReact.receivedHit(parentObject, player, mHitType);
61         					playerHitReact.hitVictim(player, parentObject, mHitType, accepted);
62         				} else {
63         					// hit the player
64         					boolean accepted = playerHitReact.receivedHit(player, parentObject, mHitType);
65         					mHitReact.hitVictim(parentObject, player, mHitType, accepted);
66         				}
67         			}
68         		}
69         	}
70         }
71     }
72 
setup(float distance, HitReactionComponent hitReact, int hitType, boolean hitPlayer)73     public void setup(float distance, HitReactionComponent hitReact, int hitType, boolean hitPlayer) {
74     	mDistance2 = distance * distance;
75     	mHitReact = hitReact;
76     	mHitType = hitType;
77     	mHitDirection = hitPlayer;
78     }
79 }
80