1
|
package com.threedcell.sokoban;
|
2
|
|
3
|
import android.graphics.Bitmap;
|
4
|
import android.graphics.Canvas;
|
5
|
|
6
|
import android.util.Log;
|
7
|
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
public class SokobanLevelBuffer
|
11
|
{
|
12
|
public static int INVALID = -1;
|
13
|
|
14
|
private static int numBuffers;
|
15
|
private static int levelHeight;
|
16
|
private static int scrHeight;
|
17
|
|
18
|
private static int[] lD;
|
19
|
private static int[] rD;
|
20
|
|
21
|
public static Canvas[] lC;
|
22
|
public static Canvas[] rC;
|
23
|
public static Bitmap[] lB;
|
24
|
public static Bitmap[] rB;
|
25
|
|
26
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27
|
|
28
|
public SokobanLevelBuffer(int lh, int sh)
|
29
|
{
|
30
|
int tmp1 = (sh-SokobanLevels.mGap);
|
31
|
int tmp2 = tmp1/lh;
|
32
|
|
33
|
numBuffers = (tmp2*lh == tmp1 ? tmp2+1:tmp2+2);
|
34
|
|
35
|
lB = new Bitmap[numBuffers];
|
36
|
rB = new Bitmap[numBuffers];
|
37
|
lD = new int[numBuffers];
|
38
|
rD = new int[numBuffers];
|
39
|
lC = new Canvas[numBuffers];
|
40
|
rC = new Canvas[numBuffers];
|
41
|
|
42
|
allocate(lh,sh);
|
43
|
}
|
44
|
|
45
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
46
|
|
47
|
|
48
|
public static void allocate(int lh,int sh)
|
49
|
{
|
50
|
levelHeight= lh;
|
51
|
scrHeight = sh;
|
52
|
|
53
|
for(int i=0; i<numBuffers; i++)
|
54
|
{
|
55
|
lB[i] = Bitmap.createBitmap( SokobanLevels.backSmallW, SokobanLevels.backSmallH, Bitmap.Config.ARGB_8888 );
|
56
|
rB[i] = Bitmap.createBitmap( SokobanLevels.levelBigW , SokobanLevels.backSmallH, Bitmap.Config.ARGB_8888 );
|
57
|
}
|
58
|
|
59
|
invalidateAllL();
|
60
|
invalidateAllR();
|
61
|
|
62
|
for(int i=0; i<numBuffers; i++)
|
63
|
{
|
64
|
lC[i] = new Canvas(lB[i]);
|
65
|
rC[i] = new Canvas(rB[i]);
|
66
|
}
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
public static int isLBuffered(int lvl)
|
72
|
{
|
73
|
for(int i=0; i<numBuffers; i++) if( lD[i]==lvl ) return i;
|
74
|
return INVALID;
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
public static int isRBuffered(int lvl)
|
80
|
{
|
81
|
for(int i=0; i<numBuffers; i++) if( rD[i]==lvl ) return i;
|
82
|
return INVALID;
|
83
|
}
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
|
87
|
public static void setLBuffered(int num,int lvl)
|
88
|
{
|
89
|
lD[num]=lvl;
|
90
|
}
|
91
|
|
92
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
93
|
|
94
|
public static void setRBuffered(int num,int lvl)
|
95
|
{
|
96
|
rD[num]=lvl;
|
97
|
}
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
public static void invalidateAllL()
|
102
|
{
|
103
|
for(int i=0; i<numBuffers; i++)
|
104
|
{
|
105
|
lD[i]=INVALID;
|
106
|
lB[i].eraseColor(0);
|
107
|
}
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
public static void invalidateAllR()
|
113
|
{
|
114
|
for(int i=0; i<numBuffers; i++)
|
115
|
{
|
116
|
rD[i]=INVALID;
|
117
|
rB[i].eraseColor(0);
|
118
|
}
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
|
123
|
public static int whereL(int lvl)
|
124
|
{
|
125
|
int scrollPos = SokobanLevels.getScroll();
|
126
|
|
127
|
int beg = 1+Math.max( scrollPos/levelHeight, 0);
|
128
|
int end = 1+(scrollPos+scrHeight-SokobanLevels.mGap)/levelHeight;
|
129
|
|
130
|
for(int i=0; i<numBuffers; i++)
|
131
|
if( lD[i]<beg || lD[i]>end ) return i;
|
132
|
|
133
|
Log.e("Buffer", "whereL lvl="+lvl+" beg="+beg+" end="+end+" returning -1");
|
134
|
|
135
|
return INVALID;
|
136
|
}
|
137
|
|
138
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139
|
|
140
|
public static int whereR(int lvl)
|
141
|
{
|
142
|
int scrollPos = SokobanLevels.getScroll();
|
143
|
|
144
|
int beg = 1+Math.max( scrollPos/levelHeight, 0);
|
145
|
int end = 1+(scrollPos+scrHeight-SokobanLevels.mGap)/levelHeight;
|
146
|
|
147
|
for(int i=0; i<numBuffers; i++)
|
148
|
if( rD[i]<beg || rD[i]>end ) return i;
|
149
|
|
150
|
Log.e("Buffer", "whereR lvl="+lvl+" beg="+beg+" end="+end+" returning -1");
|
151
|
|
152
|
return INVALID;
|
153
|
}
|
154
|
|
155
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156
|
// end of SokobanLevelBuffer class
|
157
|
}
|