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
17package com.replica.replicaisland;
18
19import com.replica.replicaisland.SoundSystem.Sound;
20
21public class GhostComponent extends GameComponent {
22    private float mMovementSpeed;
23    private float mJumpImpulse;
24    private float mAcceleration;
25    private boolean mUseOrientationSensor;
26    private float mDelayOnRelease;
27    private boolean mKillOnRelease;
28    private GameObject.ActionType mTargetAction;
29    private float mLifeTime;
30    private boolean mChangeActionOnButton;
31    private GameObject.ActionType mButtonPressedAction;
32    private Sound mAmbientSound;
33    private int mAmbientSoundStream;
34
35    public GhostComponent() {
36        super();
37        setPhase(GameComponent.ComponentPhases.THINK.ordinal());
38        reset();
39    }
40
41    @Override
42    public void reset() {
43        mMovementSpeed = 0.0f;
44        mJumpImpulse = 0.0f;
45        mAcceleration = 0.0f;
46        mUseOrientationSensor = false;
47        mDelayOnRelease = 0.0f;
48        mKillOnRelease = false;
49        mTargetAction = GameObject.ActionType.MOVE;
50        mLifeTime = 0.0f;
51        mChangeActionOnButton = false;
52        mButtonPressedAction = GameObject.ActionType.INVALID;
53        mAmbientSound = null;
54        mAmbientSoundStream = -1;
55    }
56
57    @Override
58    public void update(float timeDelta, BaseObject parent) {
59        GameObject parentObject = (GameObject) parent;
60        boolean timeToRelease = false;
61        final InputGameInterface input = sSystemRegistry.inputGameInterface;
62        final CameraSystem camera = sSystemRegistry.cameraSystem;
63
64        if (parentObject.life > 0) {
65
66            if (mLifeTime > 0.0f) {
67                mLifeTime -= timeDelta;
68                if (mLifeTime <= 0.0f) {
69                    timeToRelease = true;
70                } else if (mLifeTime < 1.0f) {
71                    // Do we have a sprite we can fade out?
72                    SpriteComponent sprite = parentObject.findByClass(SpriteComponent.class);
73                    if (sprite != null) {
74                        sprite.setOpacity(mLifeTime);
75                    }
76                }
77            }
78
79            if (parentObject.getPosition().y < -parentObject.height) {
80                // we fell off the bottom of the screen, die.
81                parentObject.life = 0;
82                timeToRelease = true;
83            }
84
85            parentObject.setCurrentAction(mTargetAction);
86            if (camera != null) {
87                camera.setTarget(parentObject);
88            }
89
90            if (input != null) {
91
92                if (mUseOrientationSensor) {
93                	final InputXY tilt = input.getTilt();
94                    parentObject.getTargetVelocity().x =
95                    	tilt.getX() * mMovementSpeed;
96
97                    parentObject.getTargetVelocity().y =
98                    	tilt.getY() * mMovementSpeed;
99
100                    parentObject.getAcceleration().x = mAcceleration;
101                    parentObject.getAcceleration().y = mAcceleration;
102                } else {
103                	final InputXY dpad = input.getDirectionalPad();
104                    parentObject.getTargetVelocity().x =
105                    	dpad.getX() * mMovementSpeed;
106
107                    parentObject.getAcceleration().x = mAcceleration;
108                }
109
110                final InputButton jumpButton = input.getJumpButton();
111                final TimeSystem time = sSystemRegistry.timeSystem;
112                final float gameTime = time.getGameTime();
113
114                if (jumpButton.getTriggered(gameTime)
115                        && parentObject.touchingGround()
116                        && parentObject.getVelocity().y <= 0.0f
117                        && !mChangeActionOnButton) {
118                    parentObject.getImpulse().y += mJumpImpulse;
119                } else if (mChangeActionOnButton && jumpButton.getPressed()) {
120                	parentObject.setCurrentAction(mButtonPressedAction);
121                }
122
123                final InputButton attackButton = input.getAttackButton();
124
125                if (attackButton.getTriggered(gameTime)) {
126                    timeToRelease = true;
127                }
128            }
129
130            if (!timeToRelease && mAmbientSound != null && mAmbientSoundStream == -1) {
131            	SoundSystem sound = BaseObject.sSystemRegistry.soundSystem;
132            	if (sound != null) {
133            		mAmbientSoundStream = sound.play(mAmbientSound, true, SoundSystem.PRIORITY_NORMAL);
134            	}
135            }
136        }
137
138        if (parentObject.life == 0) {
139        	if (mAmbientSoundStream > -1) {
140            	SoundSystem sound = BaseObject.sSystemRegistry.soundSystem;
141            	if (sound != null) {
142            		sound.stop(mAmbientSoundStream);
143            		mAmbientSoundStream = -1;
144            	}
145            }
146        }
147
148        if (timeToRelease) {
149            releaseControl(parentObject);
150        }
151
152    }
153
154    public final void releaseControl(GameObject parentObject) {
155        GameObjectManager manager = sSystemRegistry.gameObjectManager;
156        GameObject player = null;
157        if (manager != null) {
158            player = manager.getPlayer();
159        }
160
161        final CameraSystem camera = sSystemRegistry.cameraSystem;
162        if (camera != null) {
163            camera.setTarget(null);
164        }
165
166        if (player != null) {
167
168            if (mKillOnRelease) {
169                parentObject.life = 0;
170            } else {
171                // See if there's a component swap we can run.
172                ChangeComponentsComponent swap = parentObject.findByClass(ChangeComponentsComponent.class);
173                if (swap != null) {
174                    swap.activate(parentObject);
175                }
176            }
177
178            PlayerComponent control = player.findByClass(PlayerComponent.class);
179            if (camera.pointVisible(player.getPosition(), player.width)) {
180                control.deactivateGhost(0.0f);
181            } else {
182                control.deactivateGhost(mDelayOnRelease);
183            }
184           /* final InputSystem input = sSystemRegistry.inputSystem;
185            if (input != null) {
186                input.clearClickTriggered();
187            }*/
188        }
189
190        if (mAmbientSoundStream > -1) {
191        	SoundSystem sound = BaseObject.sSystemRegistry.soundSystem;
192        	if (sound != null) {
193        		sound.stop(mAmbientSoundStream);
194        		mAmbientSoundStream = -1;
195        	}
196        }
197    }
198
199    public final void setMovementSpeed(float movementSpeed) {
200        mMovementSpeed = movementSpeed;
201    }
202
203    public final void setJumpImpulse(float jumpImpulse) {
204        mJumpImpulse = jumpImpulse;
205    }
206
207    public final void setAcceleration(float accceleration) {
208        mAcceleration = accceleration;
209    }
210
211    public final void setUseOrientationSensor(boolean useSensor) {
212        mUseOrientationSensor = useSensor;
213    }
214
215    public final void setDelayOnRelease(float delayOnRelease) {
216        mDelayOnRelease = delayOnRelease;
217    }
218
219    public final void setKillOnRelease(boolean killOnRelease) {
220        mKillOnRelease = killOnRelease;
221    }
222
223    public final void setTargetAction(GameObject.ActionType action) {
224        mTargetAction = action;
225    }
226
227    public final void setLifeTime(float lifeTime) {
228        mLifeTime = lifeTime;
229    }
230
231    public final void changeActionOnButton(GameObject.ActionType pressedAction) {
232        mButtonPressedAction = pressedAction;
233        mChangeActionOnButton = true;
234    }
235
236    public final void setAmbientSound(Sound sound) {
237    	mAmbientSound = sound;
238    }
239}
240