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 /**
     20  * Adjusts the scroll position of a drawable object based on the camera's focus position.
     21  * May be used to scroll a ScrollableBitmap or TiledWorld to match the camera.  Uses DrawableFactory
     22  * to allocate fire-and-forget drawable objects every frame.
     23  */
     24 public class ScrollerComponent extends GameComponent {
     25     private int mWidth;
     26     private int mHeight;
     27     private float mHalfWidth;
     28     private float mHalfHeight;
     29     private RenderComponent mRenderComponent;
     30     private float mSpeedX;
     31     private float mSpeedY;
     32     private Texture mTexture;
     33     private TiledVertexGrid mVertGrid;
     34 
     35     public ScrollerComponent(float speedX, float speedY, int width, int height, Texture texture) {
     36         super();
     37         reset();
     38         setup(speedX, speedY, width, height);
     39         setUseTexture(texture);
     40         setPhase(ComponentPhases.PRE_DRAW.ordinal());
     41     }
     42 
     43     public ScrollerComponent(float speedX, float speedY, int width, int height, TiledVertexGrid grid) {
     44         super();
     45         reset();
     46         setup(speedX, speedY, width, height);
     47         mVertGrid = grid;
     48         setPhase(ComponentPhases.PRE_DRAW.ordinal());
     49     }
     50 
     51     public ScrollerComponent() {
     52         super();
     53         reset();
     54         setPhase(ComponentPhases.PRE_DRAW.ordinal());
     55     }
     56 
     57     @Override
     58     public void reset() {
     59         mWidth = 0;
     60         mHeight = 0;
     61         mHalfWidth = 0.0f;
     62         mHalfHeight = 0.0f;
     63         mRenderComponent = null;
     64         mSpeedX = 0.0f;
     65         mSpeedY = 0.0f;
     66         mTexture = null;
     67         mVertGrid = null;
     68     }
     69 
     70     public void setScrollSpeed(float speedX, float speedY) {
     71         mSpeedX = speedX;
     72         mSpeedY = speedY;
     73     }
     74 
     75     public void setup(float speedX, float speedY, int width, int height) {
     76         mSpeedX = speedX;
     77         mSpeedY = speedY;
     78         mWidth = width;
     79         mHeight = height;
     80         mHalfWidth = sSystemRegistry.contextParameters.gameWidth / 2.0f; //width / 2.0f;
     81         mHalfHeight = sSystemRegistry.contextParameters.gameHeight / 2.0f; //height / 2.0f;
     82     }
     83 
     84     public void setUseTexture(Texture texture) {
     85         mTexture = texture;
     86     }
     87 
     88     @Override
     89     public void update(float timeDelta, BaseObject parent) {
     90         final DrawableFactory drawableFactory = sSystemRegistry.drawableFactory;
     91         if (mRenderComponent != null && drawableFactory != null) {
     92             ScrollableBitmap background;
     93             if (mVertGrid != null) {
     94                 TiledBackgroundVertexGrid bg = drawableFactory.allocateTiledBackgroundVertexGrid();
     95                 bg.setGrid(mVertGrid);
     96                 background = bg;
     97             } else {
     98                 background = drawableFactory.allocateScrollableBitmap();
     99                 background.setTexture(mTexture);
    100             }
    101 
    102             background.setWidth(mWidth);
    103             background.setHeight(mHeight);
    104 
    105             CameraSystem camera = sSystemRegistry.cameraSystem;
    106 
    107             float originX = camera.getFocusPositionX() - mHalfWidth;
    108             float originY = camera.getFocusPositionY() - mHalfHeight;
    109 
    110             originX *= mSpeedX;
    111             originY *= mSpeedY;
    112 
    113             background.setScrollOrigin(originX, originY);
    114             mRenderComponent.setDrawable(background);
    115         }
    116     }
    117 
    118     public void setRenderComponent(RenderComponent render) {
    119         mRenderComponent = render;
    120     }
    121 }
    122