/
Effect.java
118 lines (109 loc) · 3.94 KB
/
Effect.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
/*
* Copyright (C) 2013-2014 Byron 3D Games Studio (www.b3dgs.com) Pierre-Alexandre (contact@b3dgs.com)
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package com.b3dgs.lionengine.example.game.effect;
import com.b3dgs.lionengine.UtilRandom;
import com.b3dgs.lionengine.anim.AnimState;
import com.b3dgs.lionengine.anim.Animation;
import com.b3dgs.lionengine.core.Core;
import com.b3dgs.lionengine.core.Graphic;
import com.b3dgs.lionengine.core.Media;
import com.b3dgs.lionengine.drawable.Drawable;
import com.b3dgs.lionengine.drawable.SpriteAnimated;
import com.b3dgs.lionengine.game.CameraGame;
import com.b3dgs.lionengine.game.ContextGame;
import com.b3dgs.lionengine.game.FactoryObjectGame;
import com.b3dgs.lionengine.game.ObjectGame;
import com.b3dgs.lionengine.game.SetupSurfaceGame;
import com.b3dgs.lionengine.game.configurer.ConfigAnimations;
import com.b3dgs.lionengine.game.configurer.ConfigFrames;
import com.b3dgs.lionengine.game.configurer.Configurer;
/**
* Effect base implementation.
*
* @author Pierre-Alexandre (contact@b3dgs.com)
*/
abstract class Effect
extends ObjectGame
{
/**
* Get an effect configuration file.
*
* @param type The config associated class.
* @return The media config.
*/
protected static Media getConfig(Class<? extends Effect> type)
{
return Core.MEDIA.create(type.getSimpleName() + "." + FactoryObjectGame.FILE_DATA_EXTENSION);
}
/** Surface. */
private final SpriteAnimated sprite;
/** Explode animation. */
private final Animation animExplode;
/**
* Constructor.
*
* @param setup the setup reference.
*/
protected Effect(SetupSurfaceGame setup)
{
super(setup);
// Data are loaded from the XML file, depending of the type
final Configurer configurer = setup.getConfigurer();
final ConfigFrames framesData = ConfigFrames.create(configurer);
final ConfigAnimations configAnimations = ConfigAnimations.create(configurer);
animExplode = configAnimations.getAnimation("explode");
sprite = Drawable.loadSpriteAnimated(setup.surface, framesData.getHorizontal(), framesData.getVertical());
sprite.load(false);
sprite.scale(UtilRandom.getRandomInteger(75) + 50);
setSize(sprite.getFrameWidth(), sprite.getFrameHeight());
}
/**
* Start the effect at the specified location.
*
* @param x The horizontal location.
* @param y The vertical location.
*/
public void start(int x, int y)
{
setLocation(x - getWidth() / 2, y + getHeight() / 2);
sprite.play(animExplode);
}
/*
* Effect
*/
@Override
public void prepare(ContextGame context)
{
// Nothing to do
}
@Override
public void update(double extrp)
{
sprite.updateAnimation(extrp);
// Destroy the effect when the animation is done
if (sprite.getAnimState() == AnimState.FINISHED)
{
destroy();
}
}
@Override
public void render(Graphic g, CameraGame camera)
{
sprite.render(g, camera.getViewpointX(getLocationIntX()), camera.getViewpointY(getLocationIntY()));
}
}