summaryrefslogtreecommitdiff
path: root/src/com/replica/replicaisland/TheSourceComponent.java
blob: a3a2feb2707206474271edcee7c38fba5624bf9b (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * 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;

import com.replica.replicaisland.GameObject.ActionType;

public class TheSourceComponent extends GameComponent {
	public final static float SHAKE_TIME = 0.6f;
	private final static float DIE_TIME = 30.0f;
	private final static float EXPLOSION_TIME = 0.1f;
	private final static float SHAKE_MAGNITUDE = 5.0f;
	private final static float SHAKE_SCALE = 300.0f;
	private final static float CAMERA_HIT_SHAKE_MAGNITUDE = 3.0f;

	private final static float SINK_SPEED = -20.0f;
	private float mTimer;
	private float mExplosionTimer;
	private float mShakeStartPosition;
	private ChannelSystem.Channel mChannel;
	private int mGameEvent;
	private int mGameEventIndex;
	private boolean mDead;
	
	private static ChannelSystem.ChannelBooleanValue sChannelValue = new ChannelSystem.ChannelBooleanValue();
	
	public TheSourceComponent() {
		super();
        reset();
        setPhase(ComponentPhases.THINK.ordinal());
	}
	
	@Override
	public void reset() {
		mTimer = 0.0f;
		mExplosionTimer = 0.0f;
		mShakeStartPosition = 0.0f;
		mChannel = null;
		sChannelValue.value = false;
		mGameEvent = -1;
		mGameEventIndex = -1;
		mDead = false;
	}

	@Override
	public void update(float timeDelta, BaseObject parent) {
		GameObject parentObject = (GameObject)parent;
		GameObject.ActionType currentAction = parentObject.getCurrentAction();

		CameraSystem camera = sSystemRegistry.cameraSystem;

		if (currentAction == ActionType.HIT_REACT) {
			if (parentObject.life > 0) {
				mTimer = SHAKE_TIME;
				camera.shake(SHAKE_TIME, CAMERA_HIT_SHAKE_MAGNITUDE);
				mShakeStartPosition = parentObject.getPosition().x;
				parentObject.setCurrentAction(ActionType.IDLE); 
				currentAction = ActionType.IDLE;
			} else {
				parentObject.setCurrentAction(ActionType.DEATH);
				currentAction = ActionType.DEATH;
				mTimer = DIE_TIME;
				mExplosionTimer = EXPLOSION_TIME;
				if (mChannel != null) {
					mChannel.value = sChannelValue;
					sChannelValue.value = true;
				}
				mDead = true;
			}
			
		}
		
		mTimer -= timeDelta;

		if (mDead) {
			// Wait for the player to take the camera back, then steal it!
			GameObjectManager manager = sSystemRegistry.gameObjectManager;

			if (camera != null && manager != null && camera.getTarget() == manager.getPlayer()) {
				camera.setTarget(parentObject);
			}
			
			final float offset = SINK_SPEED * timeDelta;
			parentObject.getPosition().y += offset;
			
			mExplosionTimer -= timeDelta;
			if (mExplosionTimer < 0.0f) {
				GameObjectFactory factory = sSystemRegistry.gameObjectFactory;
				if (factory != null) {
					float x = ((float)Math.random() - 0.5f) * (parentObject.width * 0.75f);
					float y = ((float)Math.random() - 0.5f) * (parentObject.height * 0.75f);
					GameObject object =
						factory.spawn(GameObjectFactory.GameObjectType.EXPLOSION_GIANT, 
							parentObject.getCenteredPositionX() + x, 
							parentObject.getCenteredPositionY() + y, 
							false);
					if (object != null) {
						manager.add(object);
					}
					mExplosionTimer = EXPLOSION_TIME;
				}
			}
				
			if (mTimer - timeDelta <= 0.0f) {
				mTimer = 0.0f;
				if (mGameEvent != -1) {
					HudSystem hud = sSystemRegistry.hudSystem;
    	        	if (hud != null) {
    	        		hud.startFade(false, 1.5f);
    	        		hud.sendGameEventOnFadeComplete(mGameEvent, mGameEventIndex);
    	        		mGameEvent = -1;
    	        	}
	    		}
			}
		} else if (mTimer > 0) {
			// shake
			float delta = (float)Math.sin(mTimer * SHAKE_SCALE);
			delta *= SHAKE_MAGNITUDE;
			parentObject.getPosition().x = mShakeStartPosition + delta;
			if (mTimer - timeDelta <= 0.0f) {
				// end one step early and fix the position.
				mTimer = 0;
				parentObject.getPosition().x = mShakeStartPosition;
			}
		}
	}
	
	public void setChannel(ChannelSystem.Channel channel) {
		mChannel = channel;
	}

	public void setGameEvent(int event, int index) {
		mGameEvent = event;
		mGameEventIndex = index;
	}
}