2 * Copyright (c) 2010 The Desert team
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 package org
.sourceforge
.desert
;
28 import java
.awt
.Dimension
;
29 import java
.util
.ArrayList
;
30 import java
.util
.Iterator
;
31 import java
.util
.List
;
33 import static java
.lang
.Math
.*;
34 import static org
.sourceforge
.desert
.Utilities
.*;
38 * @author codistmonk (creation 2010-04-27)
40 public abstract class AbstractParticleEngineCustomImplementation
implements ParticleEngine
{
42 private final List
<Particle
> particles
;
44 private float gravity
;
46 private Dimension boardSize
;
48 public AbstractParticleEngineCustomImplementation() {
49 this.particles
= new ArrayList
<Particle
>();
51 this.setGravity(DEFAULT_GRAVITY
);
55 public final void addParticle(final Particle
.Type type
, final float x
, final float y
, final float speedX
, final float speedY
) {
56 final ParticleDefaultImplementation particle
= new ParticleDefaultImplementation(type
, x
, y
, speedX
, speedY
);
58 this.particles
.add(particle
);
59 this.particleAdded(particle
);
63 public final float getGravity() {
68 public final void setGravity(final float gravity
) {
69 this.gravity
= gravity
;
73 public final Dimension
getBoardSize() {
74 return this.boardSize
;
78 public final void setBoardSize(final Dimension size
) {
79 this.boardSize
= size
;
80 this.boardSizeChanged();
84 public final int getParticleCount() {
85 return this.particles
.size();
89 public final Iterator
<Particle
> iterator() {
90 return ((Iterable
<Particle
>) this.particles
).iterator();
94 public final void removeAllParticles() {
95 this.particles
.clear();
101 * <br>Should not be null
102 * <br>Reference parameter
104 protected void particleAdded(final ParticleDefaultImplementation particle
) {
105 // Default implementation
109 protected void boardSizeChanged() {
110 // Default implementation
115 * Constrains the particle inside the bounding rectangle if it exists.
117 * <br>Should not be null
118 * <br>Input-output parameter
120 protected final void constrainPosition(final ParticleDefaultImplementation particle
) {
121 if (this.getBoardSize() != null) {
123 if (particle
.getX() < 0F
) {
125 particle
.setSpeedX(abs(particle
.getSpeedX()));
128 if (particle
.getY() < 0F
) {
130 particle
.setSpeedY(abs(particle
.getSpeedY()));
133 if (particle
.getX() >= this.getBoardSize().width
) {
134 particle
.setX(this.getBoardSize().width
- 1F
);
135 particle
.setSpeedX(-abs(particle
.getSpeedX()));
138 if (particle
.getY() >= this.getBoardSize().height
) {
139 particle
.setY(this.getBoardSize().height
- 1F
);
140 particle
.setSpeedY(-abs(particle
.getSpeedY()));
145 private static final float ARBITRARY_MAXIMUM_SPEED
= 32F
;
147 private static final float ARBITRARY_SPEED_DECAY
= 0.99F
;
152 * <br>Should not be null
153 * <br>Input-output parameter
155 protected static final void constrainSpeed(final ParticleDefaultImplementation particle
) {
156 final float speed
= length(particle
.getSpeedX(), particle
.getSpeedY());
158 if (speed
> ARBITRARY_MAXIMUM_SPEED
) {
159 particle
.setSpeedX(particle
.getSpeedX() / speed
* ARBITRARY_MAXIMUM_SPEED
);
160 particle
.setSpeedY(particle
.getSpeedY() / speed
* ARBITRARY_MAXIMUM_SPEED
);
163 particle
.setSpeedX(particle
.getSpeedX() * ARBITRARY_SPEED_DECAY
);
164 particle
.setSpeedY(particle
.getSpeedY() * ARBITRARY_SPEED_DECAY
);