Subversion Repositories AndroidProjects

Rev

Rev 74 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
73 chris 1
package com.gebauz.pingK.game;
2
 
3
import java.util.Vector;
4
 
5
import android.util.Log;
6
 
7
import com.gebauz.framework.util.Sprite2D;
8
import com.gebauz.framework.util.Vector2;
9
import com.gebauz.pingK.R;
10
 
11
public class Background
12
{
13
        public class Monkey
14
        {
15
                private float x;
16
                private float y;
17
                private float mRotation;
18
            private float mSize;
19
            private float mIntensity;
20
            private float mMoveX;
21
            private float mMoveY;
22
            private Sprite2D mSprite = null;
23
 
24
            public Monkey(float posX, float posY, float size, float intensity, Sprite2D sprite)
25
            {
26
                x = posX;
27
                y = posY;
28
                mRotation = mGameLogic.getRandomFloat(0.0f, 360.0f);
29
                mSize = size;
30
                mIntensity = intensity;
31
                mMoveX = mGameLogic.getRandomFloat(-GameConsts.MONKEY_MOVE_SPEED, GameConsts.MONKEY_MOVE_SPEED);
32
                mMoveY = mGameLogic.getRandomFloat(-GameConsts.MONKEY_MOVE_SPEED, GameConsts.MONKEY_MOVE_SPEED);
33
 
34
                mSprite = sprite;
35
            }      
36
 
37
            public void update(float deltaTime)
38
            {
39
                        x += mMoveX * deltaTime;
40
                        y += mMoveY * deltaTime;
41
 
42
                        mRotation += mMonkeySpeed * deltaTime;
43
 
44
                        float maxX = GameConsts.VIRTUAL_SCREEN_WIDTH + 50.0f;
45
                        float maxY = GameConsts.VIRTUAL_SCREEN_HEIGHT + 50.0f;
46
 
47
                        if (x < -50.0f) { x = maxX; }
48
                        if (y < -50.0f) { y = maxY; }
49
                        if (x > maxX) { x = -50.0f; }
50
                        if (y > maxY ) { y = -50.0f; }
51
            }
52
 
53
            public void render()
54
            {
92 chris 55
                Sprite2D theSprite = mSprite;
56
                if (mGameLogic.getPowerUpEffect().isFlowerActive())
57
                {
58
                        theSprite = mFlower;
59
                        theSprite.setColor(GameConsts.MONKEY_COLOR.x, GameConsts.MONKEY_COLOR.y, GameConsts.MONKEY_COLOR.z, mIntensity * GameConsts.FLOWER_MAX_INTENSITY);
60
                }
61
                else
62
                {
63
                        theSprite.setColor(GameConsts.MONKEY_COLOR.x, GameConsts.MONKEY_COLOR.y, GameConsts.MONKEY_COLOR.z, mIntensity * GameConsts.MONKEY_MAX_INTENSITY);
64
                }
73 chris 65
 
92 chris 66
                theSprite.x = x;
67
                theSprite.y = y;
68
                theSprite.angle = mRotation;
69
                theSprite.w = mSize;
70
                theSprite.h = mSize;
71
                theSprite.pivotX = mSize / 2.0f;
72
                theSprite.pivotY = mSize / 2.0f;               
73
 
74
                theSprite.render();
73 chris 75
            }
76
        }
77
 
78
        public static final int SPRITE_MONKEY = 0;
79
        public static final int SPRITE_BANANA = 1;
80
 
81
        private GameLogic mGameLogic = null;
82
 
83
        private Sprite2D mSprites[] = new Sprite2D[2];
84
        private Vector<Monkey> mMonkeys = new Vector<Monkey>();
85
        private float mMonkeySpeed = 0.0f;
86
 
92 chris 87
    private Sprite2D mFlower = null;
88
 
73 chris 89
        public Background(GameLogic gameLogic)
90
        {
91
                mGameLogic = gameLogic;
92
                mSprites[SPRITE_MONKEY] = new Sprite2D();
93
                mSprites[SPRITE_BANANA] = new Sprite2D();
94
 
95
                mSprites[SPRITE_MONKEY].init(R.drawable.monkey);
96
                mSprites[SPRITE_BANANA].init(R.drawable.banana);
97
 
92 chris 98
                mFlower = new Sprite2D();
99
                mFlower.init(R.drawable.flower);
100
 
73 chris 101
                createMonkeys();
102
        }
103
 
104
        public void createMonkeys()
105
        {
74 chris 106
                Vector2 interval = new Vector2((GameConsts.VIRTUAL_SCREEN_WIDTH + 50.0f) / (float)GameConsts.MONKEY_COUNT_X, (GameConsts.VIRTUAL_SCREEN_HEIGHT + 50.0f) / (float)GameConsts.MONKEY_COUNT_Y);
73 chris 107
 
108
                for (int i = 0; i < 2; i++)
109
                {
110
                        for (int x = 0; x < GameConsts.MONKEY_COUNT_X; x++)
111
                        {
112
                                for (int y = 0; y < GameConsts.MONKEY_COUNT_Y; y++)
113
                                {
114
                                        if ( (((x % 2) == (y % 2)) && (i == 0)) ||
115
                                                 (((x % 2) != (y % 2)) && (i == 1)) )
116
                                        {
74 chris 117
                                                float posX = -50.0f + interval.x * (float)x + mGameLogic.getRandomFloat(0.0f, interval.x * GameConsts.MONKEY_POSITION_SPREAD_FACTOR);
118
                                                float posY = -50.0f + interval.y * (float)y + mGameLogic.getRandomFloat(0.0f, interval.y * GameConsts.MONKEY_POSITION_SPREAD_FACTOR);
73 chris 119
 
120
                                                Monkey monkey = new Monkey(posX, posY, mGameLogic.getRandomFloat(GameConsts.MONKEY_MAX_SIZE, GameConsts.MONKEY_MAX_SIZE), mGameLogic.getRandomFloat(0.7f, 1.0f), mSprites[i]);
121
                                                mMonkeys.add(monkey);
122
                                        }
123
                                }
124
                        }
125
                }
126
        }
127
 
128
        public void update(float deltaTime)
129
        {
130
                for (int i = 0; i < mMonkeys.size(); i++)
131
                {
132
                        mMonkeys.get(i).update(deltaTime);
133
                }
134
        }
135
 
136
        public void render()
137
        {
138
                for (int i = 0; i < mMonkeys.size(); i++)
139
                {
140
                        mMonkeys.get(i).render();                      
141
                }              
142
        }
143
 
144
        public void setMonkeyRotationSpeed(float angularSpeed)
145
        {
146
                //Log.v(GameConsts.LOG_TAG, "monkeyspeed=" + angularSpeed);
147
                mMonkeySpeed = angularSpeed * GameConsts.MONKEY_SPEED_FACTOR;
148
        }
149
}