-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Heap.java
383 lines (320 loc) · 8.87 KB
/
Heap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Pixel Dungeon
* Copyright (C) 2012-2015 Oleg Dolya
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package com.watabou.pixeldungeon.items;
import java.util.Collection;
import java.util.LinkedList;
import com.watabou.noosa.audio.Sample;
import com.watabou.noosa.tweeners.AlphaTweener;
import com.watabou.pixeldungeon.Assets;
import com.watabou.pixeldungeon.Badges;
import com.watabou.pixeldungeon.Dungeon;
import com.watabou.pixeldungeon.Statistics;
import com.watabou.pixeldungeon.actors.buffs.Buff;
import com.watabou.pixeldungeon.actors.buffs.Burning;
import com.watabou.pixeldungeon.actors.buffs.Frost;
import com.watabou.pixeldungeon.actors.hero.Hero;
import com.watabou.pixeldungeon.actors.mobs.Mimic;
import com.watabou.pixeldungeon.actors.mobs.Wraith;
import com.watabou.pixeldungeon.effects.CellEmitter;
import com.watabou.pixeldungeon.effects.Speck;
import com.watabou.pixeldungeon.effects.Splash;
import com.watabou.pixeldungeon.effects.particles.ElmoParticle;
import com.watabou.pixeldungeon.effects.particles.FlameParticle;
import com.watabou.pixeldungeon.effects.particles.ShadowParticle;
import com.watabou.pixeldungeon.items.food.ChargrilledMeat;
import com.watabou.pixeldungeon.items.food.FrozenCarpaccio;
import com.watabou.pixeldungeon.items.food.MysteryMeat;
import com.watabou.pixeldungeon.items.scrolls.Scroll;
import com.watabou.pixeldungeon.plants.Plant.Seed;
import com.watabou.pixeldungeon.sprites.ItemSprite;
import com.watabou.pixeldungeon.sprites.ItemSpriteSheet;
import com.watabou.pixeldungeon.utils.GLog;
import com.watabou.utils.Bundlable;
import com.watabou.utils.Bundle;
import com.watabou.utils.Random;
public class Heap implements Bundlable {
private static final String TXT_MIMIC = "This is a mimic!";
private static final int SEEDS_TO_POTION = 3;
private static final float FADE_TIME = 0.6f;
public enum Type {
HEAP,
FOR_SALE,
CHEST,
LOCKED_CHEST,
CRYSTAL_CHEST,
TOMB,
SKELETON,
MIMIC,
HIDDEN
}
public Type type = Type.HEAP;
public int pos = 0;
public ItemSprite sprite;
public LinkedList<Item> items = new LinkedList<Item>();
public int image() {
switch (type) {
case HEAP:
case FOR_SALE:
return size() > 0 ? items.peek().image() : 0;
case CHEST:
case MIMIC:
return ItemSpriteSheet.CHEST;
case LOCKED_CHEST:
return ItemSpriteSheet.LOCKED_CHEST;
case CRYSTAL_CHEST:
return ItemSpriteSheet.CRYSTAL_CHEST;
case TOMB:
return ItemSpriteSheet.TOMB;
case SKELETON:
return ItemSpriteSheet.BONES;
case HIDDEN:
return ItemSpriteSheet.HIDDEN;
default:
return 0;
}
}
public ItemSprite.Glowing glowing() {
return (type == Type.HEAP || type == Type.FOR_SALE) && items.size() > 0 ? items.peek().glowing() : null;
}
public void open( Hero hero ) {
switch (type) {
case MIMIC:
if (Mimic.spawnAt( pos, items ) != null) {
GLog.n( TXT_MIMIC );
destroy();
} else {
type = Type.CHEST;
}
break;
case TOMB:
Wraith.spawnAround( hero.pos );
break;
case SKELETON:
CellEmitter.center( pos ).start( Speck.factory( Speck.RATTLE ), 0.1f, 3 );
for (Item item : items) {
if (item.cursed) {
if (Wraith.spawnAt( pos ) == null) {
hero.sprite.emitter().burst( ShadowParticle.CURSE, 6 );
hero.damage( hero.HP / 2, this );
}
Sample.INSTANCE.play( Assets.SND_CURSED );
break;
}
}
break;
case HIDDEN:
sprite.alpha( 0 );
sprite.parent.add( new AlphaTweener( sprite, 1, FADE_TIME ) );
break;
default:
}
if (type != Type.MIMIC) {
type = Type.HEAP;
sprite.link();
sprite.drop();
}
}
public int size() {
return items.size();
}
public Item pickUp() {
Item item = items.removeFirst();
if (items.isEmpty()) {
destroy();
} else if (sprite != null) {
sprite.view( image(), glowing() );
}
return item;
}
public Item peek() {
return items.peek();
}
public void drop( Item item ) {
if (item.stackable) {
Class<?> c = item.getClass();
for (Item i : items) {
if (i.getClass() == c) {
i.quantity += item.quantity;
item = i;
break;
}
}
items.remove( item );
}
if (item instanceof Dewdrop) {
items.add( item );
} else {
items.addFirst( item );
}
if (sprite != null) {
sprite.view( image(), glowing() );
}
}
public void replace( Item a, Item b ) {
int index = items.indexOf( a );
if (index != -1) {
items.remove( index );
items.add( index, b );
}
}
public void burn() {
if (type == Type.MIMIC) {
Mimic m = Mimic.spawnAt( pos, items );
if (m != null) {
Buff.affect( m, Burning.class ).reignite( m );
m.sprite.emitter().burst( FlameParticle.FACTORY, 5 );
destroy();
}
}
if (type != Type.HEAP) {
return;
}
boolean burnt = false;
boolean evaporated = false;
for (Item item : items.toArray( new Item[0] )) {
if (item instanceof Scroll) {
items.remove( item );
burnt = true;
} else if (item instanceof Dewdrop) {
items.remove( item );
evaporated = true;
} else if (item instanceof MysteryMeat) {
replace( item, ChargrilledMeat.cook( (MysteryMeat)item ) );
burnt = true;
}
}
if (burnt || evaporated) {
if (Dungeon.visible[pos]) {
if (burnt) {
burnFX( pos );
} else {
evaporateFX( pos );
}
}
if (isEmpty()) {
destroy();
} else if (sprite != null) {
sprite.view( image(), glowing() );
}
}
}
public void freeze() {
if (type == Type.MIMIC) {
Mimic m = Mimic.spawnAt( pos, items );
if (m != null) {
Buff.prolong( m, Frost.class, Frost.duration( m ) * Random.Float( 1.0f, 1.5f ) );
destroy();
}
}
if (type != Type.HEAP) {
return;
}
boolean frozen = false;
for (Item item : items.toArray( new Item[0] )) {
if (item instanceof MysteryMeat) {
replace( item, FrozenCarpaccio.cook( (MysteryMeat)item ) );
frozen = true;
}
}
if (frozen) {
if (isEmpty()) {
destroy();
} else if (sprite != null) {
sprite.view( image(), glowing() );
}
}
}
public Item transmute() {
CellEmitter.get( pos ).burst( Speck.factory( Speck.BUBBLE ), 3 );
Splash.at( pos, 0xFFFFFF, 3 );
float chances[] = new float[items.size()];
int count = 0;
int index = 0;
for (Item item : items) {
if (item instanceof Seed) {
count += item.quantity;
chances[index++] = item.quantity;
} else {
count = 0;
break;
}
}
if (count >= SEEDS_TO_POTION) {
CellEmitter.get( pos ).burst( Speck.factory( Speck.WOOL ), 6 );
Sample.INSTANCE.play( Assets.SND_PUFF );
if (Random.Int( count ) == 0) {
CellEmitter.center( pos ).burst( Speck.factory( Speck.EVOKE ), 3 );
destroy();
Statistics.potionsCooked++;
Badges.validatePotionsCooked();
return Generator.random( Generator.Category.POTION );
} else {
Seed proto = (Seed)items.get( Random.chances( chances ) );
Class<? extends Item> itemClass = proto.alchemyClass;
destroy();
Statistics.potionsCooked++;
Badges.validatePotionsCooked();
if (itemClass == null) {
return Generator.random( Generator.Category.POTION );
} else {
try {
return itemClass.newInstance();
} catch (Exception e) {
return null;
}
}
}
} else {
return null;
}
}
public static void burnFX( int pos ) {
CellEmitter.get( pos ).burst( ElmoParticle.FACTORY, 6 );
Sample.INSTANCE.play( Assets.SND_BURNING );
}
public static void evaporateFX( int pos ) {
CellEmitter.get( pos ).burst( Speck.factory( Speck.STEAM ), 5 );
}
public boolean isEmpty() {
return items == null || items.size() == 0;
}
public void destroy() {
Dungeon.level.heaps.remove( this.pos );
if (sprite != null) {
sprite.kill();
}
items.clear();
items = null;
}
private static final String POS = "pos";
private static final String TYPE = "type";
private static final String ITEMS = "items";
@SuppressWarnings("unchecked")
@Override
public void restoreFromBundle( Bundle bundle ) {
pos = bundle.getInt( POS );
type = Type.valueOf( bundle.getString( TYPE ) );
items = new LinkedList<Item>( (Collection<? extends Item>) bundle.getCollection( ITEMS ) );
}
@Override
public void storeInBundle( Bundle bundle ) {
bundle.put( POS, pos );
bundle.put( TYPE, type.toString() );
bundle.put( ITEMS, items );
}
}