rgss3 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+
2
+ require_relative 'sprite_container'
3
+ class Sprite
4
+
5
+ include RGSS3::SpriteContainer
6
+
7
+ attr_reader :bush_opacity
8
+ attr_accessor :x, :y
9
+ attr_accessor :src_rect
10
+ attr_accessor :wave_amp, :wave_length, :wave_speed, :wave_phase
11
+ attr_accessor :angle, :mirror
12
+ attr_accessor :bush_depth
13
+
14
+ def initialize(viewport = nil)
15
+ @x = 0
16
+ @y = 0
17
+ @angle = 0
18
+ @mirror = false
19
+ @bush_depth = 0
20
+ @bush_opacity = 128
21
+ @wave_speed = 360
22
+ @src_rect = Rect.new(0, 0, 0, 0)
23
+ super()
24
+ end
25
+
26
+ def flash(color, duration)
27
+ @flash_color = color || Color.new(0, 0, 0, 0)
28
+ @duration = duration
29
+ end
30
+
31
+ def update
32
+ @duration = [@duration - 1, 0].max
33
+ @flash_color = nil if @duration == 0
34
+ end
35
+
36
+ def width
37
+ @src_rect.width
38
+ end
39
+
40
+ def height
41
+ @src_rect.height
42
+ end
43
+
44
+ def bush_opacity=(int)
45
+ @bush_opacity = [[int, 255].min, 0].max
46
+ end
47
+
48
+ def bitmap=(bitmap)
49
+ super(bitmap)
50
+ @src_rect = Rect.new(0, 0, bitmap.width, bitmap.height)
51
+ end
52
+
53
+ def draw
54
+ return if !@visible || @opacity == 0 || @bitmap == nil
55
+ @bitmap.gosu_image.draw_rot(@x, @y, @z, @angle, 0.0, 0.0, @zoom_x * (@mirror ? -1 : 1), @zoom_y, 0xffffffff, BLEND[@blend_type])
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'container'
2
+ module RGSS3
3
+ # common methods for Plane and Sprite
4
+ module SpriteContainer
5
+ include Container
6
+
7
+ attr_accessor :zoom_x, :zoom_y
8
+ attr_accessor :bitmap
9
+ attr_accessor :color, :blend_type
10
+
11
+ BLEND = {0 => :default, 1 => :additive, 2 => :subtractive}
12
+
13
+ def initialize(viewport = nil)
14
+ @viewport = viewport
15
+ @zoom_x = @zoom_y = 1.0
16
+ @blend_type = 0
17
+ @color = Color.new
18
+ super()
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,80 @@
1
+ class Table
2
+
3
+ attr_accessor :data
4
+ attr_reader :xsize, :ysize, :zsize
5
+
6
+ def initialize(x, y = 0, z = 0)
7
+ init_attr(x, y, z)
8
+ @data = Array.new(@xsize * @ysize * @zsize, 0)
9
+ end
10
+
11
+ def [](x, y = 0, z = 0)
12
+ @data[x + y * @xsize + z * @xsize * @ysize]
13
+ end
14
+
15
+ def resize(x, y = 0, z = 0)
16
+ new_table = Table.new(x, y, z)
17
+ x.times do |i|
18
+ [y, 1].max.times do |j|
19
+ [z, 1].max.times do |k|
20
+ new_table[i, j, k] = self[i, j, k] || 0
21
+ end
22
+ end
23
+ end
24
+ @data = new_table.data
25
+ init_attr(x, y, z)
26
+ end
27
+
28
+
29
+ def []=(x, y = 0, z = 0, v)
30
+ @data[x + y * @xsize + z * @xsize * @ysize] = v
31
+ end
32
+
33
+ def _dump(d = 0)
34
+ s = [@dim, @xsize, @ysize, @zsize, @xsize * @ysize * @zsize].pack('LLLLL')
35
+ a = []
36
+ ta = []
37
+ @data.each do |d|
38
+ if d.is_a?(Fixnum) && (d < 32768 && d >= 0)
39
+ s << [d].pack("S")
40
+ else
41
+ s << [ta].pack("S#{ta.size}")
42
+ ni = a.size
43
+ a << d
44
+ s << [0x8000|ni].pack("S")
45
+ end
46
+ end
47
+ if a.size > 0
48
+ s << Marshal.dump(a)
49
+ end
50
+ s
51
+ end
52
+
53
+ def self._load(s)
54
+ size, nx, ny, nz, items = *s[0, 20].unpack('LLLLL')
55
+ t = Table.new(*[nx, ny, nz].first(size))
56
+ d = s[20, items * 2].unpack("S#{items}")
57
+ tail_offset = 20 + items * 2
58
+ if s.length > tail_offset
59
+ a = Marshal.load(s[tail_offset..-1])
60
+ d.collect! do |i|
61
+ if i & 0x8000 == 0x8000
62
+ a[i&~0x8000]
63
+ else
64
+ i
65
+ end
66
+ end
67
+ end
68
+ t.data = d
69
+ t
70
+ end
71
+
72
+ private
73
+
74
+ def init_attr(x, y, z)
75
+ @dim = 1 + (y > 0 ? 1 : 0) + (z > 0 ? 1 : 0)
76
+ @xsize = x
77
+ @ysize = [y, 1].max
78
+ @zsize = [z, 1].max
79
+ end
80
+ end
@@ -0,0 +1,310 @@
1
+ class Tilemap
2
+
3
+ TILESIZE = 32
4
+
5
+ attr_accessor :bitmaps
6
+ attr_reader :map_data
7
+ attr_accessor :flash_data
8
+ attr_accessor :flags
9
+ attr_accessor :viewport
10
+ attr_accessor :visible
11
+ attr_reader :ox
12
+ attr_reader :oy
13
+
14
+ def initialize(viewport = nil)
15
+ @bitmaps = []
16
+ @viewport = viewport
17
+ @visible = true
18
+ @ox = 0
19
+ @oy = 0
20
+ @animated_layer = []
21
+ @layers = [Plane.new, Plane.new, Plane.new]
22
+ @anim_count = 0
23
+ @disposed = false
24
+ @layers[0].z = 0
25
+ @layers[0].viewport = @viewport
26
+ @layers[1].z = 100
27
+ @layers[1].viewport = @viewport
28
+ @layers[2].z = 200
29
+ @layers[2].viewport = @viewport
30
+ end
31
+
32
+ def dispose
33
+ for layer in @layers
34
+ layer.bitmap.dispose
35
+ layer.dispose
36
+ end
37
+ for layer in @animated_layer
38
+ layer.dispose
39
+ end
40
+ @disposed = true
41
+ end
42
+
43
+ def disposed?
44
+ @disposed
45
+ end
46
+
47
+ def update
48
+ @anim_count = (@anim_count + 1) % (@animated_layer.size * 30)
49
+ @layers[0].bitmap = @animated_layer[@anim_count/30]
50
+ end
51
+
52
+ def refresh
53
+ return if @map_data.nil? || @flags.nil?
54
+ for layer in @layers
55
+ layer.bitmap.dispose if layer.bitmap
56
+ end
57
+ draw_animated_layer
58
+ draw_upper_layers
59
+ end
60
+
61
+ def draw_animated_layer
62
+ bitmap = Bitmap.new(@map_data.xsize * TILESIZE, @map_data.ysize * TILESIZE)
63
+ if need_animated_layer?
64
+ @animated_layer = [bitmap, bitmap.clone, bitmap.clone]
65
+ else
66
+ @animated_layer = [bitmap]
67
+ end
68
+ @layers[0].bitmap = @animated_layer[0]
69
+ for x in 0..@map_data.xsize - 1
70
+ for y in 0..@map_data.ysize - 1
71
+ draw_A1tile(x,y,@map_data[x,y,0],true) if @map_data[x,y,0].between?(2048,2815)
72
+ draw_A2tile(x,y,@map_data[x,y,0]) if @map_data[x,y,0].between?(2816,4351)
73
+ draw_A3tile(x,y,@map_data[x,y,0]) if @map_data[x,y,0].between?(4352,5887)
74
+ draw_A4tile(x,y,@map_data[x,y,0]) if @map_data[x,y,0].between?(5888,8191)
75
+ draw_A5tile(x,y,@map_data[x,y,0]) if @map_data[x,y,0].between?(1536,1663)
76
+ end
77
+ end
78
+ for x in 0..@map_data.xsize - 1
79
+ for y in 0..@map_data.ysize - 1
80
+ draw_A1tile(x,y,@map_data[x,y,1],true) if @map_data[x,y,1].between?(2048,2815)
81
+ draw_A2tile(x,y,@map_data[x,y,1]) if @map_data[x,y,1].between?(2816,4351)
82
+ end
83
+ end
84
+ end
85
+
86
+ def bitmap_for_autotile(autotile)
87
+ return 0 if autotile.between?(0,15)
88
+ return 1 if autotile.between?(16,47)
89
+ return 2 if autotile.between?(48,79)
90
+ return 3 if autotile.between?(80,127)
91
+ end
92
+
93
+ A1 = [
94
+ [13,14,17,18], [2,14,17,18], [13,3,17,18], [2,3,17,18],
95
+ [13,14,17,7], [2,14,17,7], [13,3,17,7], [2,3,17,7],
96
+ [13,14,6,18], [2,14,6,18], [13,3,6,18], [2,3,6,18],
97
+ [13,14,6,7], [2,14,6,7], [13,3,6,7], [2,3,6,7],
98
+ [12,14,16,18], [12,3,16,18], [12,14,16,7], [12,3,16,7],
99
+ [9,10,17,18], [9,10,17,7], [9,10,6,18], [9,10,6,7],
100
+ [13,15,17,19], [13,15,6,19], [2,15,17,19], [2,15,6,19],
101
+ [13,14,21,22], [2,14,21,22], [13,3,21,22], [2,3,21,22],
102
+ [12,15,16,19], [9,10,21,22], [8,9,12,13], [8,9,12,7],
103
+ [10,11,14,15], [10,11,6,15], [18,19,22,23], [2,19,22,23],
104
+ [16,17,20,21], [16,3,20,21], [8,11,12,15], [8,9,20,21],
105
+ [16,19,20,23], [10,11,22,23], [8,11,20,23], [0,1,4,5]
106
+ ]
107
+
108
+ A1POS = [
109
+ [0,0],[0,TILESIZE*3],[TILESIZE*6,0],[TILESIZE*6,TILESIZE*3],
110
+ [TILESIZE*8,0],[TILESIZE*14,0],[TILESIZE*8,TILESIZE*3],[TILESIZE*14,TILESIZE*3],
111
+ [0,TILESIZE*6],[TILESIZE*6,TILESIZE*6],[0,TILESIZE*9],[TILESIZE*6,TILESIZE*9],
112
+ [TILESIZE*8,TILESIZE*6],[TILESIZE*14,TILESIZE*6],[TILESIZE*8,TILESIZE*9],[TILESIZE*14,TILESIZE*9]
113
+ ]
114
+
115
+ def draw_A1tile(x,y,id,animated = false)
116
+ autotile = (id - 2048) / 48
117
+ return draw_waterfalltile(x,y,id) if [5,7,9,11,13,15].include?(autotile)
118
+ index = (id - 2048) % 48
119
+ case bitmap_for_autotile(autotile)
120
+ when 0
121
+ x2 = A1POS[autotile][0]
122
+ y2 = A1POS[autotile][1]
123
+ when 1
124
+ x2 = (TILESIZE * 2) * ((autotile - 16) % 8)
125
+ y2 = (TILESIZE * 3) * ((autotile - 16) / 8)
126
+ when 2
127
+ x2 = (TILESIZE * 2) * ((autotile - 48) % 8)
128
+ y2 = (TILESIZE * 2) * ((autotile - 48) / 8)
129
+ when 3
130
+ x2 = (TILESIZE * 2) * ((autotile - 80) % 8)
131
+ y2 = (TILESIZE * 3) * ((((autotile - 80) / 8)+1)/2) + (TILESIZE * 2) * (((autotile - 80) / 8)/2)
132
+ end
133
+ rect = Rect.new(0,0,TILESIZE/2,TILESIZE/2)
134
+ for layer in @animated_layer
135
+ for i in 0..3
136
+ rect.x = x2 + (TILESIZE/2) * (A1[index][i] % 4)
137
+ rect.y = y2 + (TILESIZE/2) * (A1[index][i] / 4)
138
+ case i
139
+ when 0
140
+ layer.blt(x * TILESIZE, y * TILESIZE,@bitmaps[bitmap_for_autotile(autotile)],rect)
141
+ when 1
142
+ layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE,@bitmaps[bitmap_for_autotile(autotile)],rect)
143
+ when 2
144
+ layer.blt(x * TILESIZE, y * TILESIZE + (TILESIZE/2),@bitmaps[bitmap_for_autotile(autotile)],rect)
145
+ when 3
146
+ layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE + (TILESIZE/2),@bitmaps[bitmap_for_autotile(autotile)],rect)
147
+ end
148
+ end
149
+ x2 += TILESIZE * 2 if animated && ![2,3].include?(autotile)
150
+ end
151
+ end
152
+
153
+ A1E = [
154
+ [0,1,6,7],[0,1,4,5],[2,3,6,7],[1,2,5,6]
155
+ ]
156
+
157
+ def draw_waterfalltile(x,y,id)
158
+ autotile = (id - 2048) / 48
159
+ index = (id - 2048) % 48
160
+ x2 = A1POS[autotile][0]
161
+ y2 = A1POS[autotile][1]
162
+ rect = Rect.new(0,0,TILESIZE/2,TILESIZE/2)
163
+ for layer in @animated_layer
164
+ for i in 0..3
165
+ rect.x = x2 + (TILESIZE/2) * (A1E[index][i] % 4)
166
+ rect.y = y2 + (TILESIZE/2) * (A1E[index][i] / 4)
167
+ case i
168
+ when 0
169
+ layer.blt(x * TILESIZE, y * TILESIZE,@bitmaps[bitmap_for_autotile(autotile)],rect)
170
+ when 1
171
+ layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE,@bitmaps[0],rect)
172
+ when 2
173
+ layer.blt(x * TILESIZE, y * TILESIZE + (TILESIZE/2),@bitmaps[0],rect)
174
+ when 3
175
+ layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE + (TILESIZE/2),@bitmaps[0],rect)
176
+ end
177
+ end
178
+ y2 += TILESIZE
179
+ end
180
+ end
181
+
182
+ def draw_A2tile(x,y,id)
183
+ draw_A1tile(x,y,id)
184
+ end
185
+
186
+ A3 = [
187
+ [5,6,9,10], [4,5,8,9], [1,2,5,6], [0,1,4,5],
188
+ [6,7,10,11], [4,7,8,11], [2,3,6,7], [0,3,4,7],
189
+ [9,10,13,14], [8,9,12,13], [1,2,13,14], [0,1,12,13],
190
+ [10,11,14,15], [8,11,12,13], [2,3,14,15], [0,3,12,15]
191
+ ]
192
+
193
+ def draw_A3tile(x,y,id)
194
+ autotile = (id - 2048) / 48
195
+ index = (id - 2048) % 48
196
+ case bitmap_for_autotile(autotile)
197
+ when 0
198
+ x2 = (TILESIZE * 2) * ((autotile) % 8)
199
+ y2 = (TILESIZE * 3) * ((autotile) / 8)
200
+ when 1
201
+ x2 = (TILESIZE * 2) * ((autotile - 16) % 8)
202
+ y2 = (TILESIZE * 3) * ((autotile - 16) / 8)
203
+ when 2
204
+ x2 = (TILESIZE * 2) * ((autotile - 48) % 8)
205
+ y2 = (TILESIZE * 2) * ((autotile - 48) / 8)
206
+ when 3
207
+ x2 = (TILESIZE * 2) * ((autotile - 80) % 8)
208
+ y2 = (TILESIZE * 3) * ((((autotile - 80) / 8)+1)/2) + (TILESIZE * 2) * (((autotile - 80) / 8)/2)
209
+ end
210
+ rect = Rect.new(0,0,TILESIZE/2,TILESIZE/2)
211
+ for layer in @animated_layer
212
+ for i in 0..3
213
+ if A3[index].nil?
214
+ rect.x = x2 + (TILESIZE/2) * (A1[index][i] % 4)
215
+ rect.y = y2 + (TILESIZE/2) * (A1[index][i] / 4)
216
+ else
217
+ rect.x = x2 + (TILESIZE/2) * (A3[index][i] % 4)
218
+ rect.y = y2 + (TILESIZE/2) * (A3[index][i] / 4)
219
+ end
220
+ case i
221
+ when 0
222
+ layer.blt(x * TILESIZE, y * TILESIZE,@bitmaps[bitmap_for_autotile(autotile)],rect)
223
+ when 1
224
+ layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE,@bitmaps[bitmap_for_autotile(autotile)],rect)
225
+ when 2
226
+ layer.blt(x * TILESIZE, y * TILESIZE + (TILESIZE/2),@bitmaps[bitmap_for_autotile(autotile)],rect)
227
+ when 3
228
+ layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE + (TILESIZE/2),@bitmaps[bitmap_for_autotile(autotile)],rect)
229
+ end
230
+ end
231
+ end
232
+ end
233
+
234
+ def draw_A4tile(x,y,id)
235
+ autotile = (id - 2048) / 48
236
+ case autotile
237
+ when 80..87
238
+ draw_A1tile(x,y,id)
239
+ when 96..103
240
+ draw_A1tile(x,y,id)
241
+ when 112..119
242
+ draw_A1tile(x,y,id)
243
+ else
244
+ draw_A3tile(x,y,id)
245
+ end
246
+ end
247
+
248
+ def draw_A5tile(x,y,id)
249
+ id -= 1536
250
+ rect = Rect.new(TILESIZE * (id % 8),TILESIZE * ((id % 128) / 8),TILESIZE,TILESIZE)
251
+ for layer in @animated_layer
252
+ layer.blt(x * TILESIZE, y * TILESIZE,@bitmaps[4],rect)
253
+ end
254
+ end
255
+
256
+ def need_animated_layer?
257
+ for x in 0..@map_data.xsize - 1
258
+ for y in 0..@map_data.ysize - 1
259
+ if @map_data[x,y,0].between?(2048, 2815)
260
+ return true
261
+ end
262
+ end
263
+ end
264
+ return false
265
+ end
266
+
267
+ def draw_upper_layers
268
+ bitmap = Bitmap.new(@map_data.xsize * TILESIZE, @map_data.ysize * TILESIZE)
269
+ @layers[1].bitmap = bitmap
270
+ @layers[2].bitmap = bitmap.clone
271
+ rect = Rect.new(0,0,TILESIZE,TILESIZE)
272
+ for x in 0..@map_data.xsize - 1
273
+ for y in 0..@map_data.ysize - 1
274
+ n = @map_data[x,y,2] % 256
275
+ rect.x = TILESIZE * ((n % 8) + (8 * (n / 128)))
276
+ rect.y = TILESIZE * ((n % 128) / 8)
277
+ if @flags[@map_data[x,y,2]] & 0x10 == 0
278
+ @layers[1].bitmap.blt(x * TILESIZE, y * TILESIZE,@bitmaps[5+@map_data[x,y,2]/256],rect)
279
+ else
280
+ @layers[2].bitmap.blt(x * TILESIZE, y * TILESIZE,@bitmaps[5+@map_data[x,y,2]/256],rect)
281
+ end
282
+ end
283
+ end
284
+ end
285
+
286
+ def map_data=(data)
287
+ return if @map_data == data
288
+ @map_data = data
289
+ refresh
290
+ end
291
+
292
+ def flags=(data)
293
+ @flags = data
294
+ refresh
295
+ end
296
+
297
+ def ox=(value)
298
+ @ox = value
299
+ for layer in @layers
300
+ layer.ox = @ox
301
+ end
302
+ end
303
+
304
+ def oy=(value)
305
+ @oy = value
306
+ for layer in @layers
307
+ layer.oy = @oy
308
+ end
309
+ end
310
+ end