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 19/** 20 * Maintains a canonical time step, in seconds, for the entire game engine. This time step 21 * represents real changes in time but is only updated once per frame. 22 */ 23// TODO: time distortion effects could go here, or they could go into a special object manager. 24public class TimeSystem extends BaseObject { 25 private float mGameTime; 26 private float mRealTime; 27 private float mFreezeDelay; 28 private float mGameFrameDelta; 29 private float mRealFrameDelta; 30 31 private float mTargetScale; 32 private float mScaleDuration; 33 private float mScaleStartTime; 34 private boolean mEaseScale; 35 36 private static final float EASE_DURATION = 0.5f; 37 38 public TimeSystem() { 39 super(); 40 reset(); 41 } 42 43 @Override 44 public void reset() { 45 mGameTime = 0.0f; 46 mRealTime = 0.0f; 47 mFreezeDelay = 0.0f; 48 mGameFrameDelta = 0.0f; 49 mRealFrameDelta = 0.0f; 50 51 mTargetScale = 1.0f; 52 mScaleDuration = 0.0f; 53 mScaleStartTime = 0.0f; 54 mEaseScale = false; 55 } 56 57 @Override 58 public void update(float timeDelta, BaseObject parent) { 59 mRealTime += timeDelta; 60 mRealFrameDelta = timeDelta; 61 62 if (mFreezeDelay > 0.0f) { 63 mFreezeDelay -= timeDelta; 64 mGameFrameDelta = 0.0f; 65 } else { 66 float scale = 1.0f; 67 if (mScaleStartTime > 0.0f) { 68 final float scaleTime = mRealTime - mScaleStartTime; 69 if (scaleTime > mScaleDuration) { 70 mScaleStartTime = 0; 71 } else { 72 if (mEaseScale) { 73 if (scaleTime <= EASE_DURATION) { 74 // ease in 75 scale = Lerp.ease(1.0f, mTargetScale, EASE_DURATION, scaleTime); 76 } else if (mScaleDuration - scaleTime < EASE_DURATION) { 77 // ease out 78 final float easeOutTime = EASE_DURATION - (mScaleDuration - scaleTime); 79 scale = Lerp.ease(mTargetScale, 1.0f, EASE_DURATION, easeOutTime); 80 } else { 81 scale = mTargetScale; 82 } 83 } else { 84 scale = mTargetScale; 85 } 86 } 87 } 88 89 mGameTime += (timeDelta * scale); 90 mGameFrameDelta = (timeDelta * scale); 91 } 92 93 94 } 95 96 public float getGameTime() { 97 return mGameTime; 98 } 99 100 public float getRealTime() { 101 return mRealTime; 102 } 103 104 public float getFrameDelta() { 105 return mGameFrameDelta; 106 } 107 108 public float getRealTimeFrameDelta() { 109 return mRealFrameDelta; 110 } 111 112 public void freeze(float seconds) { 113 mFreezeDelay = seconds; 114 } 115 116 public void appyScale(float scaleFactor, float duration, boolean ease) { 117 mTargetScale = scaleFactor; 118 mScaleDuration = duration; 119 mEaseScale = ease; 120 if (mScaleStartTime <= 0.0f) { 121 mScaleStartTime = mRealTime; 122 } 123 } 124 125} 126