Added ParticleEngineCellularImplementation + tests; still a lot of things to improve...
[desert.git] / src / org / sourceforge / desert / AbstractParticleEngineCustomImplementation.java
blob2152a29a91fd11f6dd3fcad76204a1c1354d3a12
1 /*
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
11 * conditions:
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.*;
36 /**
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);
54 @Override
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);
62 @Override
63 public final float getGravity() {
64 return this.gravity;
67 @Override
68 public final void setGravity(final float gravity) {
69 this.gravity = gravity;
72 @Override
73 public final Dimension getBoardSize() {
74 return this.boardSize;
77 @Override
78 public final void setBoardSize(final Dimension size) {
79 this.boardSize = size;
80 this.boardSizeChanged();
83 @Override
84 public final int getParticleCount() {
85 return this.particles.size();
88 @Override
89 public final Iterator<Particle> iterator() {
90 return ((Iterable<Particle>) this.particles).iterator();
93 @Override
94 public final void removeAllParticles() {
95 this.particles.clear();
98 /**
99 * This method should be overriden if a special action must be executed just
100 * after a particle has been added.
101 * @param particle
102 * <br>Should not be null
103 * <br>Reference parameter
105 protected void particleAdded(final ParticleDefaultImplementation particle) {
106 // Default implementation
107 // Do nothing
111 * This method should be overriden if a special action must be executed just
112 * after the board size has been changed.
114 protected void boardSizeChanged() {
115 // Default implementation
116 // Do nothing
120 * Constrains the particle inside the bounding rectangle if it exists.
121 * @param particle
122 * <br>Should not be null
123 * <br>Input-output parameter
125 protected final void constrainPosition(final ParticleDefaultImplementation particle) {
126 if (this.getBoardSize() != null) {
127 // Left bound
128 if (particle.getX() < 0F) {
129 particle.setX(0F);
130 particle.setSpeedX(abs(particle.getSpeedX()));
132 // Top bound
133 if (particle.getY() < 0F) {
134 particle.setY(0F);
135 particle.setSpeedY(abs(particle.getSpeedY()));
137 // Right bound
138 if (particle.getX() >= this.getBoardSize().width) {
139 particle.setX(this.getBoardSize().width - 1F);
140 particle.setSpeedX(-abs(particle.getSpeedX()));
142 // Bottom bound
143 if (particle.getY() >= this.getBoardSize().height) {
144 particle.setY(this.getBoardSize().height - 1F);
145 particle.setSpeedY(-abs(particle.getSpeedY()));
150 protected static final float ARBITRARY_MAXIMUM_SPEED = 32F;
152 protected static final float ARBITRARY_SPEED_DECAY = 0.99F;
156 * @param particle
157 * <br>Should not be null
158 * <br>Input-output parameter
160 protected static final void constrainSpeed(final ParticleDefaultImplementation particle) {
161 final float speed = length(particle.getSpeedX(), particle.getSpeedY());
163 if (speed > ARBITRARY_MAXIMUM_SPEED) {
164 particle.setSpeedX(particle.getSpeedX() / speed * ARBITRARY_MAXIMUM_SPEED);
165 particle.setSpeedY(particle.getSpeedY() / speed * ARBITRARY_MAXIMUM_SPEED);
167 else {
168 particle.setSpeedX(particle.getSpeedX() * ARBITRARY_SPEED_DECAY);
169 particle.setSpeedY(particle.getSpeedY() * ARBITRARY_SPEED_DECAY);