Home | History | Annotate | Download | only in replicaisland
      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 package com.replica.replicaisland;
     18 
     19 import com.replica.replicaisland.GameObject.ActionType;
     20 
     21 /**
     22  * A component that implements the "pop-out" AI behavior.  Pop-out characters alternate between
     23  * hiding and appearing based on their distance from the player.  They do not move or normally
     24  * attack.
     25  */
     26 public class SleeperComponent extends GameComponent {
     27     private final static int STATE_SLEEPING = 0;
     28     private final static int STATE_WAKING = 1;
     29     private final static int STATE_ATTACKING = 2;
     30     private final static int STATE_SLAM = 3;
     31     private final static float DEFAULT_WAKE_UP_DURATION = 3.0f;
     32     private float mWakeUpDuration;
     33     private float mStateTime;
     34     private int mState;
     35     private float mSlamDuration;
     36     private float mSlamMagnitude;
     37     private float mAttackImpulseX;
     38     private float mAttackImpulseY;
     39 
     40     public SleeperComponent() {
     41         super();
     42         setPhase(GameComponent.ComponentPhases.THINK.ordinal());
     43         reset();
     44     }
     45 
     46     @Override
     47     public void reset() {
     48         mWakeUpDuration = DEFAULT_WAKE_UP_DURATION;
     49         mState = STATE_SLEEPING;
     50         mStateTime = 0.0f;
     51         mSlamDuration = 0.0f;
     52         mSlamMagnitude = 0.0f;
     53         mAttackImpulseX = 0.0f;
     54         mAttackImpulseY = 0.0f;
     55     }
     56 
     57     @Override
     58     public void update(float timeDelta, BaseObject parent) {
     59         GameObject parentObject = (GameObject) parent;
     60 
     61         if (parentObject.getCurrentAction() == ActionType.INVALID) {
     62             parentObject.setCurrentAction(GameObject.ActionType.IDLE);
     63             mState = STATE_SLEEPING;
     64         }
     65 
     66         CameraSystem camera = sSystemRegistry.cameraSystem;
     67         switch(mState) {
     68             case STATE_SLEEPING:
     69                 if (camera.shaking() && camera.pointVisible(parentObject.getPosition(), parentObject.width / 2.0f)) {
     70                     mState = STATE_WAKING;
     71                     mStateTime = mWakeUpDuration;
     72                     parentObject.setCurrentAction(GameObject.ActionType.MOVE);
     73                 }
     74                 break;
     75             case STATE_WAKING:
     76                 mStateTime -= timeDelta;
     77                 if (mStateTime <= 0.0f) {
     78                     mState = STATE_ATTACKING;
     79                     parentObject.setCurrentAction(GameObject.ActionType.ATTACK);
     80                     parentObject.getImpulse().x += mAttackImpulseX * parentObject.facingDirection.x;
     81                     parentObject.getImpulse().y += mAttackImpulseY;
     82                 }
     83                 break;
     84             case STATE_ATTACKING:
     85                 if (parentObject.touchingGround() && parentObject.getVelocity().y < 0.0f) {
     86                     mState = STATE_SLAM;
     87                     camera.shake(mSlamDuration, mSlamMagnitude);
     88                     parentObject.getVelocity().zero();
     89                 }
     90                 break;
     91             case STATE_SLAM:
     92                 if (!camera.shaking()) {
     93                     mState = STATE_SLEEPING;
     94                     parentObject.setCurrentAction(GameObject.ActionType.IDLE);
     95                 }
     96                 break;
     97 
     98         }
     99     }
    100 
    101 
    102     public void setWakeUpDuration(float duration) {
    103         mWakeUpDuration = duration;
    104     }
    105 
    106     public void setSlam(float duration, float magnitude) {
    107         mSlamDuration = duration;
    108         mSlamMagnitude = magnitude;
    109     }
    110 
    111     public void setAttackImpulse(float x, float y) {
    112         mAttackImpulseX = x;
    113         mAttackImpulseY = y;
    114     }
    115 }
    116 
    117 
    118