├── .gitignore ├── README ├── src ├── main │ ├── resources │ │ └── maze.txt │ └── java │ │ └── com │ │ └── pavelfatin │ │ └── game │ │ ├── Layer.java │ │ ├── Space.java │ │ ├── behavior │ │ ├── Behavior.java │ │ ├── RoamingBehavior.java │ │ ├── RandomBehavior.java │ │ ├── DirectedBehavior.java │ │ ├── ControlledBehavior.java │ │ ├── StaticBehavior.java │ │ ├── GlancingBehavior.java │ │ └── AbstractBehavior.java │ │ ├── LayerComparator.java │ │ ├── entity │ │ ├── TransparencyFilter.java │ │ ├── Wall.java │ │ ├── ImmortalEnemy.java │ │ ├── RoamingDot.java │ │ ├── Booster.java │ │ ├── Fruit.java │ │ ├── Dike.java │ │ ├── Dot.java │ │ ├── Enemy.java │ │ ├── Entity.java │ │ └── Creature.java │ │ ├── display │ │ ├── FpsCounter.java │ │ ├── ControlHandler.java │ │ └── LabyrinthDisplay.java │ │ ├── SpaceAdapter.java │ │ ├── Settings.java │ │ ├── Game.java │ │ ├── Direction.java │ │ ├── Utilities.java │ │ ├── MainFrame.java │ │ ├── EntityFactory.java │ │ ├── LabyrinthLoader.java │ │ ├── Compass.java │ │ ├── Labyrinth.java │ │ └── IntersectionFinder.java └── test │ └── java │ └── com │ └── pavelfatin │ └── game │ └── IntersectionFinderTest.java ├── pom.xml └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Pacman-like game - an example of good object-oriented design. 2 | 3 | Pavel Fatin, http://pavelfatin.com -------------------------------------------------------------------------------- /src/main/resources/maze.txt: -------------------------------------------------------------------------------- 1 | ################# 2 | #F....I#....E..B# 3 | #.####.#.####.#.# 4 | #...R#.#...##.#.# 5 | ####.#.#.#.#.R..# 6 | ##.#.#...#.#.#.## 7 | ##.#.#####D#.#.## 8 | #.S#.....#.#.#.## 9 | #.##.#.#.#.#.#.## 10 | #....#.#.#.#.#..# 11 | #.####.#.#.#.##.# 12 | #.F...B#...C....# 13 | ################# -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/Layer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | public enum Layer { 21 | BOTTOM, 22 | MIDDLE, 23 | TOP 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/Space.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import java.util.Collection; 21 | 22 | 23 | public interface Space { 24 | Collection getAvailableDirections(); 25 | 26 | Direction getDirectionToward(Class type); 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/behavior/Behavior.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.behavior; 19 | 20 | 21 | import com.pavelfatin.game.Direction; 22 | import com.pavelfatin.game.Space; 23 | 24 | 25 | public interface Behavior { 26 | Direction nextDirection(Space space); 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/behavior/RoamingBehavior.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.behavior; 19 | 20 | public class RoamingBehavior extends AbstractBehavior { 21 | @Override 22 | protected void think() { 23 | if (!isMoving() || !isIntentionDirectionAvailable()) { 24 | turnToRandomDirection(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/LayerComparator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import com.pavelfatin.game.entity.Entity; 21 | 22 | import java.util.Comparator; 23 | 24 | 25 | class LayerComparator implements Comparator { 26 | public int compare(Entity one, Entity another) { 27 | return one.getLayer().compareTo(another.getLayer()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/behavior/RandomBehavior.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.behavior; 19 | 20 | public class RandomBehavior extends RoamingBehavior { 21 | private static final double TURN_PROBABILITY = 0.01D; 22 | 23 | 24 | @Override 25 | protected void think() { 26 | super.think(); 27 | 28 | if (Math.random() < TURN_PROBABILITY) { 29 | turnToRandomDirection(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/behavior/DirectedBehavior.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.behavior; 19 | 20 | import com.pavelfatin.game.entity.Creature; 21 | 22 | 23 | public class DirectedBehavior extends AbstractBehavior { 24 | @Override 25 | protected void think() { 26 | if (!isMoving() 27 | || !isIntentionDirectionAvailable() 28 | || isPerpendicularTurnsAvailable()) { 29 | turnToward(Creature.class); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/TransparencyFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import java.awt.*; 21 | import java.awt.image.RGBImageFilter; 22 | 23 | class TransparencyFilter extends RGBImageFilter { 24 | private int _markerRGB; 25 | 26 | TransparencyFilter(Color color) { 27 | _markerRGB = color.getRGB() | 0xFF000000; 28 | } 29 | 30 | @Override 31 | public final int filterRGB(int x, int y, int rgb) { 32 | return ((rgb | 0xFF000000) == _markerRGB) ? (0x00FFFFFF & rgb) : rgb; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/behavior/ControlledBehavior.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.behavior; 19 | 20 | import com.pavelfatin.game.Direction; 21 | 22 | 23 | public class ControlledBehavior extends AbstractBehavior { 24 | private Direction _nextIntention = Direction.None; 25 | 26 | 27 | public void navigate(Direction direction) { 28 | _nextIntention = direction; 29 | } 30 | 31 | @Override 32 | protected void think() { 33 | if (isDirectionAvailable(_nextIntention)) { 34 | setIntention(_nextIntention); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/behavior/StaticBehavior.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.behavior; 19 | 20 | import com.pavelfatin.game.Direction; 21 | import com.pavelfatin.game.Space; 22 | 23 | 24 | public class StaticBehavior implements Behavior { 25 | private static final Behavior _instance = new StaticBehavior(); 26 | 27 | 28 | private StaticBehavior() { 29 | } 30 | 31 | public static Behavior getInstance() { 32 | return _instance; 33 | } 34 | 35 | public Direction nextDirection(Space space) { 36 | return Direction.None; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/display/FpsCounter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.display; 19 | 20 | class FpsCounter { 21 | private static final int PERIOD = 1; 22 | 23 | private long _last; 24 | private int _frames; 25 | private int _fps; 26 | 27 | 28 | public int getFps(long time) { 29 | _frames++; 30 | 31 | int seconds = (int) ((time - _last) / 1000L); 32 | 33 | if (seconds >= PERIOD) { 34 | _fps = _frames / seconds; 35 | 36 | _frames = 0; 37 | _last = time; 38 | } 39 | 40 | return _fps; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/SpaceAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import com.pavelfatin.game.entity.Entity; 21 | 22 | import java.util.Collection; 23 | 24 | 25 | class SpaceAdapter implements Space { 26 | private Labyrinth _labyrinth; 27 | private Entity _entity; 28 | 29 | 30 | SpaceAdapter(Labyrinth labyrinth, Entity entity) { 31 | _labyrinth = labyrinth; 32 | _entity = entity; 33 | } 34 | 35 | public Collection getAvailableDirections() { 36 | return _labyrinth.getAvailableDirections(_entity); 37 | } 38 | 39 | public Direction getDirectionToward(Class type) { 40 | return _labyrinth.getDirectionToward(_entity, type); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/Wall.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import com.pavelfatin.game.behavior.StaticBehavior; 21 | 22 | import java.awt.*; 23 | 24 | 25 | public class Wall extends Entity { 26 | public Wall() { 27 | super(StaticBehavior.getInstance(), 0); 28 | } 29 | 30 | @Override 31 | public boolean canPassThrough(Entity entity) { 32 | return false; 33 | } 34 | 35 | @Override 36 | public boolean canEat(Entity entity) { 37 | return false; 38 | } 39 | 40 | @Override 41 | public void draw(Graphics2D graphics) { 42 | graphics.setColor(Color.BLACK); 43 | graphics.fillRect(1, 44 | 1, 45 | getPosition().width - 2, 46 | getPosition().height - 2); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/ImmortalEnemy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import com.pavelfatin.game.behavior.Behavior; 21 | 22 | import java.awt.*; 23 | 24 | 25 | public class ImmortalEnemy extends Enemy { 26 | public ImmortalEnemy(Behavior behavior, Color color) { 27 | super(behavior, color); 28 | } 29 | 30 | @Override 31 | public boolean canEat(Entity entity) { 32 | return entity instanceof Creature; 33 | } 34 | 35 | @Override 36 | public void draw(Graphics2D graphics) { 37 | super.draw(graphics); 38 | 39 | graphics.setColor(Color.RED); 40 | 41 | final int inset = 4; 42 | graphics.drawOval(inset, inset, 43 | getPosition().width - inset * 2, 44 | getPosition().height - inset * 2); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/RoamingDot.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import com.pavelfatin.game.Layer; 21 | import com.pavelfatin.game.behavior.Behavior; 22 | 23 | import java.awt.*; 24 | 25 | 26 | public class RoamingDot extends Dot { 27 | public RoamingDot(Behavior behavior) { 28 | super(behavior, 500); 29 | } 30 | 31 | @Override 32 | public Layer getLayer() { 33 | return Layer.BOTTOM; 34 | } 35 | 36 | @Override 37 | public void draw(Graphics2D graphics) { 38 | int halfCell = Math.round((float) getPosition().width / 2.0F); 39 | int r = getPosition().width / 7; 40 | graphics.setColor(Color.BLUE); 41 | graphics.fillOval(halfCell - r, 42 | halfCell - r, 43 | r * 2, 44 | r * 2); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/Booster.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import com.pavelfatin.game.behavior.StaticBehavior; 21 | 22 | import java.awt.*; 23 | 24 | 25 | public class Booster extends Entity { 26 | public Booster() { 27 | super(StaticBehavior.getInstance(), 0); 28 | } 29 | 30 | @Override 31 | public boolean canPassThrough(Entity entity) { 32 | return false; 33 | } 34 | 35 | @Override 36 | public boolean canEat(Entity entity) { 37 | return false; 38 | } 39 | 40 | @Override 41 | public void draw(Graphics2D graphics) { 42 | int halfCell = Math.round((float) getPosition().width / 2.0F); 43 | 44 | graphics.setColor(Color.RED); 45 | graphics.fillRect(halfCell - 1, halfCell / 2, 2, halfCell); 46 | graphics.fillRect(halfCell / 2, halfCell - 1, halfCell, 2); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/Settings.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | 21 | import java.net.URL; 22 | 23 | 24 | class Settings { 25 | private URL _url; 26 | private int _tickPeriod; 27 | private int _cellSize; 28 | private int _stepSize; 29 | 30 | 31 | Settings(URL url, int tickPeriod, int cellSize, int stepSize) { 32 | _url = url; 33 | _tickPeriod = tickPeriod; 34 | _cellSize = cellSize; 35 | _stepSize = stepSize; 36 | } 37 | 38 | public URL getURL() { 39 | return _url; 40 | } 41 | 42 | public int getTickPeriod() { 43 | return _tickPeriod; 44 | } 45 | 46 | public int getCellSize() { 47 | return _cellSize; 48 | } 49 | 50 | public int getStepSize() { 51 | return _stepSize; 52 | } 53 | 54 | static Settings getDefault() { 55 | return new Settings(MainFrame.class.getResource("/maze.txt"), 10, 36, 2); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/behavior/GlancingBehavior.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.behavior; 19 | 20 | import com.pavelfatin.game.Direction; 21 | 22 | 23 | public class GlancingBehavior extends RoamingBehavior { 24 | private static final double GLANCE_PROBABILITY = 0.5D; 25 | 26 | 27 | @Override 28 | protected void think() { 29 | if (isIntentionDirectionAvailable()) { 30 | if ((Math.random() < GLANCE_PROBABILITY)) { 31 | glance(); 32 | } 33 | } else { 34 | super.think(); 35 | } 36 | } 37 | 38 | private void glance() { 39 | if (isPerpendicularTurnsAvailable()) { 40 | for (Direction direction : Direction.MOVING_DIRECTIONS) { 41 | if (isIntentionPerpendicualrTo(direction) && 42 | isDirectionAvailable(direction)) { 43 | setIntention(direction); 44 | return; 45 | } 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/Fruit.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import com.pavelfatin.game.behavior.StaticBehavior; 21 | 22 | import java.awt.*; 23 | 24 | 25 | public class Fruit extends Entity { 26 | private Color _color; 27 | 28 | 29 | public Fruit(Color color) { 30 | super(StaticBehavior.getInstance(), 1000); 31 | _color = color; 32 | } 33 | 34 | @Override 35 | public boolean canPassThrough(Entity entity) { 36 | return false; 37 | } 38 | 39 | @Override 40 | public boolean canEat(Entity entity) { 41 | return false; 42 | } 43 | 44 | @Override 45 | public void draw(Graphics2D graphics) { 46 | int xSize = getPosition().width / 2; 47 | int ySize = getPosition().height / 3; 48 | graphics.setColor(_color); 49 | graphics.fillOval(getPosition().x + (getPosition().width - xSize) / 2, 50 | getPosition().y + (getPosition().height - ySize) / 2, 51 | xSize, 52 | ySize); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/Dike.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import com.pavelfatin.game.Layer; 21 | import com.pavelfatin.game.behavior.Behavior; 22 | 23 | import java.awt.*; 24 | 25 | 26 | public class Dike extends Entity { 27 | public Dike(Behavior behavior) { 28 | super(behavior, 0); 29 | } 30 | 31 | @Override 32 | public boolean canPassThrough(Entity entity) { 33 | return !(entity instanceof Wall 34 | || entity instanceof Creature 35 | || entity instanceof Enemy 36 | || entity instanceof Dike 37 | || entity instanceof RoamingDot); 38 | } 39 | 40 | @Override 41 | public boolean canEat(Entity entity) { 42 | return false; 43 | } 44 | 45 | @Override 46 | public Layer getLayer() { 47 | return Layer.TOP; 48 | } 49 | 50 | @Override 51 | public void draw(Graphics2D graphics) { 52 | graphics.setColor(Color.LIGHT_GRAY); 53 | graphics.fillRect(4, 4, getPosition().width - 8, getPosition().height - 8); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/Game.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import javax.swing.*; 21 | import java.io.File; 22 | 23 | 24 | public class Game { 25 | public static void main(String[] args) throws Exception { 26 | if (args.length == 0) { 27 | runWith(Settings.getDefault()); 28 | } else if (args.length == 4) { 29 | runWith(settingsFrom(args)); 30 | } else { 31 | System.err.println("Usage: game.jar [ ]"); 32 | } 33 | } 34 | 35 | private static void runWith(Settings settings) { 36 | final MainFrame frame = new MainFrame(settings); 37 | 38 | SwingUtilities.invokeLater(new Runnable() { 39 | public void run() { 40 | frame.open(); 41 | } 42 | }); 43 | } 44 | 45 | private static Settings settingsFrom(String[] args) throws Exception { 46 | return new Settings(new File(args[0]).toURI().toURL(), 47 | Integer.parseInt(args[1]), 48 | Integer.parseInt(args[2]), 49 | Integer.parseInt(args[3])); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/display/ControlHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.display; 19 | 20 | import com.pavelfatin.game.Direction; 21 | import com.pavelfatin.game.Labyrinth; 22 | 23 | import java.awt.event.KeyAdapter; 24 | import java.awt.event.KeyEvent; 25 | 26 | 27 | public class ControlHandler extends KeyAdapter { 28 | private Labyrinth _labyrinth; 29 | 30 | 31 | public ControlHandler(Labyrinth labyrinth) { 32 | _labyrinth = labyrinth; 33 | } 34 | 35 | @Override 36 | public void keyPressed(KeyEvent e) { 37 | switch (e.getKeyCode()) { 38 | case KeyEvent.VK_LEFT: 39 | _labyrinth.navigate(Direction.Left); 40 | break; 41 | case KeyEvent.VK_RIGHT: 42 | _labyrinth.navigate(Direction.Right); 43 | break; 44 | case KeyEvent.VK_UP: 45 | _labyrinth.navigate(Direction.Up); 46 | break; 47 | case KeyEvent.VK_DOWN: 48 | _labyrinth.navigate(Direction.Down); 49 | break; 50 | default: 51 | // do nothing 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/Direction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import java.awt.*; 21 | 22 | 23 | public enum Direction { 24 | None(0, 0), 25 | Left(-1, 0), 26 | Right(1, 0), 27 | Up(0, -1), 28 | Down(0, 1); 29 | 30 | public static final Direction[] MOVING_DIRECTIONS = {Left, Right, Up, Down}; 31 | private Point _vector; 32 | 33 | 34 | Direction(int x, int y) { 35 | _vector = new Point(x, y); 36 | } 37 | 38 | public Rectangle translate(Rectangle rectanle, int distance) { 39 | Rectangle result = new Rectangle(rectanle); 40 | result.translate(_vector.x * distance, _vector.y * distance); 41 | return result; 42 | } 43 | 44 | public boolean isPerpendicular(Direction direction) { 45 | if ((Left.equals(this) || Right.equals(this)) 46 | && (Up.equals(direction) || Down.equals(direction))) { 47 | return true; 48 | } 49 | 50 | if ((Up.equals(this) || Down.equals(this)) 51 | && (Left.equals(direction) || Right.equals(direction))) { 52 | return true; 53 | } 54 | 55 | return false; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/Dot.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import com.pavelfatin.game.behavior.Behavior; 21 | import com.pavelfatin.game.behavior.StaticBehavior; 22 | 23 | import java.awt.*; 24 | 25 | 26 | public class Dot extends Entity { 27 | public Dot() { 28 | super(StaticBehavior.getInstance(), 100); 29 | } 30 | 31 | protected Dot(Behavior behavior, int containedScore) { 32 | super(behavior, containedScore); 33 | } 34 | 35 | @Override 36 | public boolean canPassThrough(Entity entity) { 37 | return !(entity instanceof Wall 38 | || entity instanceof Dike); 39 | } 40 | 41 | @Override 42 | public boolean canEat(Entity entity) { 43 | return false; 44 | } 45 | 46 | @Override 47 | public void draw(Graphics2D graphics) { 48 | int halfCell = Math.round((float) getPosition().width / 2.0F); 49 | int r = getPosition().width / 10; 50 | graphics.setColor(Color.BLUE); 51 | graphics.fillOval(halfCell - r, 52 | halfCell - r, 53 | r * 2, 54 | r * 2); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/Utilities.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import java.awt.*; 21 | import java.util.Collection; 22 | import java.util.Random; 23 | 24 | 25 | public class Utilities { 26 | private static final Random _random = new Random(); 27 | 28 | private Utilities() { 29 | } 30 | 31 | public static T chooseElement(T[] elements) { 32 | return chooseElement(elements, null); 33 | } 34 | 35 | public static T chooseElement(Collection elements, T alternative) { 36 | return chooseElement((T[]) (elements.toArray()), alternative); 37 | } 38 | 39 | public static T chooseElement(T[] elements, T alternative) { 40 | if (elements.length == 0) { 41 | return alternative; 42 | } else { 43 | int choiceIndex = _random.nextInt(elements.length); 44 | return elements[choiceIndex]; 45 | } 46 | } 47 | 48 | public static void centerOnScreen(Window window) { 49 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 50 | Dimension size = window.getSize(); 51 | window.setLocation( 52 | (screenSize.width - size.width) / 2, 53 | (screenSize.height - size.height) / 2); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 4.0.0 5 | com.pavelfatin 6 | game 7 | jar 8 | 1.0 9 | Game 10 | http://pavelfatin.com 11 | 12 | 13 | UTF-8 14 | 15 | 16 | 17 | 18 | junit 19 | junit 20 | 4.9 21 | test 22 | 23 | 24 | 25 | 26 | 27 | 28 | org.apache.maven.plugins 29 | maven-jar-plugin 30 | 2.3.1 31 | 32 | 33 | false 34 | 35 | com.pavelfatin.game.Game 36 | 37 | 38 | 39 | 40 | 41 | maven-assembly-plugin 42 | 43 | 44 | bin 45 | 46 | 47 | 48 | 49 | make-assembly 50 | package 51 | 52 | single 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/MainFrame.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import com.pavelfatin.game.display.ControlHandler; 21 | import com.pavelfatin.game.display.LabyrinthDisplay; 22 | 23 | import javax.swing.*; 24 | import java.awt.event.ActionEvent; 25 | import java.awt.event.ActionListener; 26 | 27 | 28 | class MainFrame extends JFrame { 29 | private Labyrinth _labyrinth; 30 | private LabyrinthDisplay _display; 31 | private Timer _timer; 32 | 33 | 34 | MainFrame(Settings settings) { 35 | super("Game"); 36 | 37 | _labyrinth = new LabyrinthLoader().load(settings.getURL(), settings.getCellSize(), settings.getStepSize()); 38 | _display = new LabyrinthDisplay(_labyrinth); 39 | getContentPane().add(_display); 40 | 41 | _display.addKeyListener(new ControlHandler(_labyrinth)); 42 | 43 | _timer = new Timer(settings.getTickPeriod(), new TimerListener()); 44 | } 45 | 46 | public void open() { 47 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 48 | pack(); 49 | setResizable(false); 50 | Utilities.centerOnScreen(this); 51 | setVisible(true); 52 | _timer.start(); 53 | } 54 | 55 | private class TimerListener implements ActionListener { 56 | public void actionPerformed(ActionEvent e) { 57 | _labyrinth.processTick(); 58 | _display.repaint(); 59 | 60 | if (_labyrinth.isWin() || _labyrinth.isLose()) { 61 | _timer.stop(); 62 | } 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/behavior/AbstractBehavior.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.behavior; 19 | 20 | import com.pavelfatin.game.Direction; 21 | import com.pavelfatin.game.Space; 22 | import com.pavelfatin.game.Utilities; 23 | 24 | import java.util.Collection; 25 | 26 | 27 | public abstract class AbstractBehavior implements Behavior { 28 | private Direction _intention = Direction.None; 29 | private Collection _availableDirections; 30 | private Space _space; 31 | 32 | 33 | public Direction nextDirection(Space space) { 34 | _space = space; 35 | _availableDirections = _space.getAvailableDirections(); 36 | 37 | think(); 38 | 39 | if (!isIntentionDirectionAvailable()) { 40 | setIntention(Direction.None); 41 | } 42 | 43 | return _intention; 44 | } 45 | 46 | protected boolean isDirectionAvailable(Direction direction) { 47 | return _availableDirections.contains(direction); 48 | } 49 | 50 | protected boolean isIntentionDirectionAvailable() { 51 | return isDirectionAvailable(_intention); 52 | } 53 | 54 | protected boolean isIntentionPerpendicualrTo(Direction direction) { 55 | return _intention.isPerpendicular(direction); 56 | } 57 | 58 | protected boolean isPerpendicularTurnsAvailable() { 59 | for (Direction direction : _availableDirections) { 60 | if (_intention.isPerpendicular(direction)) { 61 | return true; 62 | } 63 | } 64 | 65 | return false; 66 | } 67 | 68 | public void setIntention(Direction intention) { 69 | _intention = intention; 70 | } 71 | 72 | protected void turnToRandomDirection() { 73 | _intention = randomDirection(); 74 | } 75 | 76 | protected Direction randomDirection() { 77 | return Utilities.chooseElement(_availableDirections, Direction.None); 78 | } 79 | 80 | protected boolean isMoving() { 81 | return !Direction.None.equals(_intention); 82 | } 83 | 84 | protected void turnToward(Class type) { 85 | setIntention(_space.getDirectionToward(type)); 86 | } 87 | 88 | protected abstract void think(); 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/Enemy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import com.pavelfatin.game.Layer; 21 | import com.pavelfatin.game.behavior.Behavior; 22 | 23 | import java.awt.*; 24 | 25 | 26 | public class Enemy extends Entity { 27 | private Color _color; 28 | 29 | 30 | public Enemy(Behavior behavior, Color color) { 31 | super(behavior, 5000); 32 | _color = color; 33 | } 34 | 35 | @Override 36 | public boolean canPassThrough(Entity entity) { 37 | return !(entity instanceof Wall 38 | || entity instanceof Dike); 39 | } 40 | 41 | @Override 42 | public boolean canEat(Entity entity) { 43 | return entity instanceof Creature && 44 | !((Creature) entity).isBoosted(); 45 | } 46 | 47 | @Override 48 | public void draw(Graphics2D graphics) { 49 | int halfCell = Math.round((float) getPosition().width / 2.0F); 50 | int r = getPosition().width / 3; 51 | graphics.setColor(_color); 52 | graphics.fillOval(+ halfCell - r, 53 | + halfCell - r, 54 | r * 2, 55 | r * 2); 56 | 57 | int crossRadius = r / 2; 58 | graphics.setColor(Color.BLACK); 59 | graphics.drawLine(halfCell - crossRadius, 60 | halfCell - crossRadius, 61 | halfCell + crossRadius, 62 | halfCell + crossRadius); 63 | graphics.drawLine(halfCell - crossRadius - 1, 64 | halfCell - crossRadius + 1, 65 | halfCell + crossRadius + 1, 66 | halfCell + crossRadius - 1); 67 | 68 | graphics.drawLine(halfCell + crossRadius, 69 | halfCell - crossRadius, 70 | halfCell - crossRadius, 71 | halfCell + crossRadius); 72 | graphics.drawLine(halfCell + crossRadius - 1, 73 | halfCell - crossRadius - 1, 74 | halfCell - crossRadius + 1, 75 | halfCell + crossRadius + 1); 76 | } 77 | 78 | @Override 79 | public Layer getLayer() { 80 | return Layer.TOP; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/EntityFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import com.pavelfatin.game.behavior.*; 21 | import com.pavelfatin.game.entity.*; 22 | 23 | import java.awt.*; 24 | 25 | 26 | class EntityFactory { 27 | private static final Class[] BEHAVIORS 28 | = new Class[]{RoamingBehavior.class, 29 | RandomBehavior.class, 30 | GlancingBehavior.class}; 31 | 32 | private static final Color[] COLORS 33 | = new Color[]{Color.RED, Color.GREEN, Color.YELLOW, 34 | Color.CYAN, Color.PINK, Color.MAGENTA}; 35 | 36 | 37 | public Creature createCreature() { 38 | return new Creature(new ControlledBehavior()); 39 | } 40 | 41 | public Dot createDot() { 42 | return new Dot(); 43 | } 44 | 45 | public RoamingDot createRoamingDot() { 46 | return new RoamingDot(createRandomBehavior(BEHAVIORS)); 47 | } 48 | 49 | public Booster createBooster() { 50 | return new Booster(); 51 | } 52 | 53 | public Dike createDike() { 54 | return new Dike(createRandomBehavior(BEHAVIORS)); 55 | } 56 | 57 | public Fruit createFruit() { 58 | return new Fruit(Utilities.chooseElement(COLORS)); 59 | } 60 | 61 | public Wall createWall() { 62 | return new Wall(); 63 | } 64 | 65 | public Enemy createEnemy() { 66 | Color color = Utilities.chooseElement(COLORS); 67 | return new Enemy(createRandomBehavior(BEHAVIORS), color); 68 | } 69 | 70 | public Enemy createImmortalEnemy() { 71 | Color color = Utilities.chooseElement(COLORS); 72 | return new ImmortalEnemy(createRandomBehavior(BEHAVIORS), color); 73 | } 74 | 75 | public Enemy createSmartEnemy() { 76 | Color color = Utilities.chooseElement(COLORS); 77 | return new Enemy(new DirectedBehavior(), color); 78 | } 79 | 80 | private Behavior createRandomBehavior(Class[] behaviors) { 81 | try { 82 | Class behaviorClass = Utilities.chooseElement(behaviors); 83 | return (Behavior) behaviorClass.newInstance(); 84 | } catch (InstantiationException 85 | e) { 86 | throw new RuntimeException(e); 87 | } catch (IllegalAccessException 88 | e) { 89 | throw new RuntimeException(e); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/LabyrinthLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import com.pavelfatin.game.entity.Entity; 21 | 22 | import java.awt.*; 23 | import java.io.BufferedReader; 24 | import java.io.FileNotFoundException; 25 | import java.io.IOException; 26 | import java.io.InputStreamReader; 27 | import java.net.URL; 28 | 29 | 30 | class LabyrinthLoader { 31 | private static final EntityFactory _factory = new EntityFactory(); 32 | private Entity _controlledEntity; 33 | 34 | 35 | Labyrinth load(URL url, int cellSize, int stepSize) { 36 | BufferedReader reader = null; 37 | try { 38 | try { 39 | reader = new BufferedReader(new InputStreamReader(url.openStream())); 40 | return read(reader, cellSize, stepSize); 41 | } finally { 42 | if (reader != null) { 43 | reader.close(); 44 | } 45 | } 46 | } catch (FileNotFoundException e) { 47 | throw new RuntimeException(e); 48 | } catch (IOException e) { 49 | throw new RuntimeException(e); 50 | } 51 | } 52 | 53 | private Labyrinth read(BufferedReader reader, int cellSize, int stepSize) throws IOException { 54 | Labyrinth labyrinth = new Labyrinth(cellSize, stepSize); 55 | 56 | int row = 0; 57 | while (reader.ready()) { 58 | char[] chars = reader.readLine().toCharArray(); 59 | 60 | int column = 0; 61 | for (char aChar : chars) { 62 | Entity entity = createEntity(aChar); 63 | labyrinth.add(entity, new Point(column, row)); 64 | 65 | column++; 66 | } 67 | 68 | row++; 69 | } 70 | 71 | labyrinth.setControlledEntity(_controlledEntity); 72 | 73 | return labyrinth; 74 | } 75 | 76 | private Entity createEntity(char character) { 77 | switch (character) { 78 | case 'C': 79 | _controlledEntity = _factory.createCreature(); 80 | return _controlledEntity; 81 | case 'E': 82 | return _factory.createEnemy(); 83 | case 'I': 84 | return _factory.createImmortalEnemy(); 85 | case 'S': 86 | return _factory.createSmartEnemy(); 87 | case 'F': 88 | return _factory.createFruit(); 89 | case '.': 90 | return _factory.createDot(); 91 | case 'D': 92 | return _factory.createDike(); 93 | case 'R': 94 | return _factory.createRoamingDot(); 95 | case 'B': 96 | return _factory.createBooster(); 97 | case '#': 98 | return _factory.createWall(); 99 | default: 100 | throw new RuntimeException( 101 | "Unknown entity: '" + character + "'"); 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/Compass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import com.pavelfatin.game.entity.Entity; 21 | 22 | import java.awt.*; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | 27 | class Compass { 28 | private Labyrinth _labyrinth; 29 | private Entity _entity; 30 | private Class _type; 31 | 32 | private Map _edges; 33 | private Map _traces; 34 | 35 | 36 | Compass(Labyrinth labyrinth, Entity entity, Class type) { 37 | _entity = entity; 38 | _labyrinth = labyrinth; 39 | _type = type; 40 | } 41 | 42 | public Direction locate() { 43 | _edges = new HashMap(); 44 | _traces = new HashMap(); 45 | 46 | initialSplash(_entity.getPosition()); 47 | 48 | do { 49 | Direction mark = getTouch(); 50 | if (mark != null) { 51 | return mark; 52 | } 53 | 54 | flow(); 55 | } while (isSpaceLeft()); 56 | 57 | return Direction.None; 58 | } 59 | 60 | private void initialSplash(Rectangle position) { 61 | for (Direction direction : Direction.MOVING_DIRECTIONS) { 62 | raise(_labyrinth.translate(position, direction), direction); 63 | } 64 | } 65 | 66 | private void splash(Rectangle position, Direction mark) { 67 | for (Direction direction : Direction.MOVING_DIRECTIONS) { 68 | raise(_labyrinth.translate(position, direction), mark); 69 | } 70 | } 71 | 72 | private void raise(Rectangle target, Direction mark) { 73 | if (isNotInTrace(target) && isAvailable(target)) { 74 | _edges.put(target, mark); 75 | } 76 | } 77 | 78 | private void fall(Rectangle position, Direction mark) { 79 | _traces.put(position, mark); 80 | _edges.remove(position); 81 | } 82 | 83 | private void flow() { 84 | Map edges 85 | = new HashMap(_edges); 86 | 87 | for (Map.Entry edge : edges.entrySet()) { 88 | Rectangle position = edge.getKey(); 89 | Direction mark = edge.getValue(); 90 | fall(position, mark); 91 | splash(position, mark); 92 | } 93 | } 94 | 95 | private Direction getTouch() { 96 | for (Map.Entry edge : _edges.entrySet()) { 97 | if (_labyrinth.isIntersects(edge.getKey(), _type)) { 98 | return edge.getValue(); 99 | } 100 | } 101 | return null; 102 | } 103 | 104 | private boolean isAvailable(Rectangle position) { 105 | return _labyrinth.canBeAt(_entity, position); 106 | } 107 | 108 | private boolean isNotInTrace(Rectangle position) { 109 | return !_traces.containsKey(position); 110 | } 111 | 112 | private boolean isSpaceLeft() { 113 | return _edges.size() > 0; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/display/LabyrinthDisplay.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.display; 19 | 20 | import com.pavelfatin.game.Labyrinth; 21 | 22 | import javax.swing.*; 23 | import java.awt.*; 24 | import java.awt.geom.Rectangle2D; 25 | 26 | 27 | public class LabyrinthDisplay extends JComponent { 28 | private static final Font INFO_FONT = new Font("Arial", Font.BOLD, 16); 29 | private static final Font RESULT_FONT = new Font("Arial", Font.BOLD, 42); 30 | 31 | private Labyrinth _labyrinth; 32 | private FpsCounter _fpsCounter = new FpsCounter(); 33 | 34 | 35 | public LabyrinthDisplay(Labyrinth labyrinth) { 36 | _labyrinth = labyrinth; 37 | 38 | setFocusable(true); 39 | setPreferredSize(_labyrinth.getDimensions()); 40 | enableInputMethods(true); 41 | setDoubleBuffered(true); 42 | } 43 | 44 | @Override 45 | protected void paintComponent(Graphics g) { 46 | g.setColor(Color.WHITE); 47 | g.fillRect(0, 0, getVisibleRect().width, getVisibleRect().height); 48 | 49 | _labyrinth.render((Graphics2D) g); 50 | 51 | drawScore(g); 52 | drawFps(g); 53 | 54 | if (_labyrinth.isWin()) { 55 | drawResult(g, "Win", Color.ORANGE); 56 | } 57 | 58 | if (_labyrinth.isLose()) { 59 | drawResult(g, "Lose", Color.RED); 60 | } 61 | } 62 | 63 | private void drawScore(Graphics g) { 64 | drawInfo(g, String.format("Score: %d", _labyrinth.getScore()), 10, 20); 65 | } 66 | 67 | private void drawFps(Graphics g) { 68 | String info = String.format("FPS: %3d", _fpsCounter.getFps(System.currentTimeMillis())); 69 | 70 | Dimension infoSize = getSize(g, info); 71 | Dimension displaySize = getSize(); 72 | 73 | drawInfo(g, info, displaySize.width - infoSize.width - 12, 20); 74 | } 75 | 76 | private Dimension getSize(Graphics g, String string) { 77 | Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g); 78 | return new Dimension((int) bounds.getWidth(), 79 | (int) bounds.getHeight()); 80 | } 81 | 82 | private void drawInfo(Graphics g, String info, int x, int y) { 83 | g.setFont(INFO_FONT); 84 | Dimension size = getSize(g, info); 85 | 86 | g.setColor(Color.LIGHT_GRAY); 87 | g.fillRect(x - 5, y - (int) (size.height / 1.2F) - 1, 88 | size.width + 10, size.height); 89 | 90 | g.setColor(Color.BLACK); 91 | g.drawString(info, x, y); 92 | } 93 | 94 | private void drawResult(Graphics g, String result, Color color) { 95 | g.setFont(RESULT_FONT); 96 | Dimension size = getSize(g, result); 97 | 98 | g.setColor(Color.DARK_GRAY); 99 | g.fillRect(0, _labyrinth.getDimensions().height / 2 100 | - (int) (size.height / 1.2F), 101 | _labyrinth.getDimensions().width, size.height); 102 | 103 | g.setColor(color); 104 | g.drawString(result, (_labyrinth.getDimensions().width - size.width) / 2, 105 | _labyrinth.getDimensions().height / 2); 106 | } 107 | 108 | @Override 109 | public void invalidate() { 110 | } 111 | 112 | @Override 113 | public void validate() { 114 | } 115 | 116 | @Override 117 | public void revalidate() { 118 | } 119 | 120 | @Override 121 | protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 122 | } 123 | 124 | @Override 125 | public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/Entity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import com.pavelfatin.game.Direction; 21 | import com.pavelfatin.game.Layer; 22 | import com.pavelfatin.game.Space; 23 | import com.pavelfatin.game.behavior.Behavior; 24 | 25 | import java.awt.*; 26 | import java.awt.image.BufferedImage; 27 | import java.awt.image.FilteredImageSource; 28 | 29 | 30 | public abstract class Entity { 31 | private int _containedScore; 32 | private Behavior _behavior; 33 | private int _collectedScore; 34 | private boolean _eaten; 35 | private Rectangle _position; 36 | private Space _space; 37 | 38 | private boolean _drawRequired = true; 39 | private BufferedImage _image; 40 | private Image _transparentImage; 41 | private TransparencyFilter _filter = new TransparencyFilter(Color.WHITE); 42 | 43 | 44 | protected Entity(Behavior behavior, int containedScore) { 45 | _behavior = behavior; 46 | _containedScore = containedScore; 47 | } 48 | 49 | public final int getCollectedScore() { 50 | return _collectedScore; 51 | } 52 | 53 | public Behavior getBehavior() { 54 | return _behavior; 55 | } 56 | 57 | void eat(Entity entity) { 58 | _collectedScore += entity._containedScore; 59 | entity._eaten = true; 60 | } 61 | 62 | public boolean isEaten() { 63 | return _eaten; 64 | } 65 | 66 | public Direction getNextDirection() { 67 | return _behavior.nextDirection(_space); 68 | } 69 | 70 | public Layer getLayer() { 71 | return Layer.BOTTOM; 72 | } 73 | 74 | public Rectangle getPosition() { 75 | return _position; 76 | } 77 | 78 | public void setPosition(Rectangle position) { 79 | _position = position; 80 | } 81 | 82 | public void setSpace(Space space) { 83 | _space = space; 84 | } 85 | 86 | public void processIntersection(Entity another) { 87 | if (canEat(another)) { 88 | eat(another); 89 | } 90 | } 91 | 92 | public final void render(Graphics2D graphics) { 93 | if (_image == null 94 | || _image.getWidth() != _position.width 95 | || _image.getHeight() != _position.height) { 96 | createImages(); 97 | redraw(); 98 | } 99 | 100 | if (_drawRequired) { 101 | redrawImage(); 102 | } 103 | 104 | graphics.drawImage(_transparentImage, _position.x, _position.y, null); 105 | } 106 | 107 | private void redrawImage() { 108 | Graphics2D graphics = (Graphics2D) _image.getGraphics(); 109 | 110 | graphics.setColor(Color.WHITE); 111 | graphics.fillRect(0, 0, _image.getWidth(), _image.getHeight()); 112 | 113 | draw(graphics); 114 | 115 | _transparentImage = makeTransparent(_image); 116 | 117 | _drawRequired = false; 118 | } 119 | 120 | private void createImages() { 121 | GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment() 122 | .getDefaultScreenDevice().getDefaultConfiguration(); 123 | 124 | _image = configuration.createCompatibleImage(_position.width, 125 | _position.height, 126 | Transparency.BITMASK); 127 | } 128 | 129 | private Image makeTransparent(Image image) { 130 | return Toolkit.getDefaultToolkit().createImage( 131 | new FilteredImageSource(image.getSource(), _filter)); 132 | } 133 | 134 | protected void redraw() { 135 | _drawRequired = true; 136 | } 137 | 138 | public abstract boolean canPassThrough(Entity entity); 139 | 140 | protected abstract boolean canEat(Entity entity); 141 | 142 | protected abstract void draw(Graphics2D graphics); 143 | } 144 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/entity/Creature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game.entity; 19 | 20 | import com.pavelfatin.game.Direction; 21 | import com.pavelfatin.game.Layer; 22 | import com.pavelfatin.game.behavior.Behavior; 23 | 24 | import java.awt.*; 25 | import java.util.Date; 26 | 27 | 28 | public class Creature extends Entity { 29 | private static final int SECONDS_TO_BOOST = 5; 30 | 31 | private boolean _boosted; 32 | private boolean _halfBoosted; 33 | private Date _boostStartedTime; 34 | 35 | 36 | public Creature(Behavior behavior) { 37 | super(behavior, 1000); 38 | } 39 | 40 | @Override 41 | public boolean canPassThrough(Entity entity) { 42 | return !(entity instanceof Wall 43 | || entity instanceof Dike); 44 | } 45 | 46 | @Override 47 | public boolean canEat(Entity entity) { 48 | boolean isEatableThing = entity instanceof Booster 49 | || entity instanceof Dot 50 | || entity instanceof Fruit; 51 | boolean isEatableEnemy = _boosted 52 | && entity instanceof Enemy 53 | && !(entity instanceof ImmortalEnemy); 54 | return isEatableThing || isEatableEnemy; 55 | } 56 | 57 | @Override 58 | public void eat(Entity entity) { 59 | super.eat(entity); 60 | 61 | if (entity instanceof Booster) { 62 | setBoosted(true); 63 | } 64 | } 65 | 66 | private int getBoostedSeconds() { 67 | if (_boosted) { 68 | return (int) ((new Date().getTime() - _boostStartedTime.getTime()) / 1000); 69 | } else { 70 | return 0; 71 | } 72 | } 73 | 74 | @Override 75 | public Direction getNextDirection() { 76 | handleBoostedState(); 77 | return super.getNextDirection(); 78 | } 79 | 80 | private void handleBoostedState() { 81 | if (_boosted) { 82 | if (getBoostedSeconds() > SECONDS_TO_BOOST) { 83 | setBoosted(false); 84 | } 85 | 86 | if (getBoostedSeconds() >= SECONDS_TO_BOOST / 2) { 87 | if (isHalfBoosted()) { 88 | redraw(); 89 | } 90 | setHalfBoosted(false); 91 | } 92 | } 93 | } 94 | 95 | public boolean isBoosted() { 96 | return _boosted; 97 | } 98 | 99 | private void setBoosted(boolean boosted) { 100 | _boosted = boosted; 101 | _halfBoosted = boosted; 102 | 103 | _boostStartedTime = new Date(); 104 | 105 | redraw(); 106 | } 107 | 108 | private boolean isHalfBoosted() { 109 | return _halfBoosted; 110 | } 111 | 112 | private void setHalfBoosted(boolean value) { 113 | _halfBoosted = value; 114 | } 115 | 116 | @Override 117 | public void draw(Graphics2D graphics) { 118 | int halfCell = Math.round((float) getPosition().width / 2.0F); 119 | int r = getPosition().width / 3; 120 | 121 | graphics.setColor(Color.ORANGE); 122 | graphics.fillOval(halfCell - r, halfCell - r, r * 2, r * 2); 123 | 124 | graphics.setColor(isBoosted() ? Color.RED : Color.BLACK); 125 | graphics.fillOval(halfCell - (int) (r / 2.0F), 126 | halfCell - (int) (r / 2.5F), 127 | 3, 3); 128 | graphics.fillOval(halfCell + (int) (r / 2.0F) - 3, 129 | halfCell - (int) (r / 2.5F), 130 | 3, 3); 131 | 132 | if (!isHalfBoosted()) { 133 | graphics.setColor(Color.BLACK); 134 | } 135 | 136 | graphics.drawArc(halfCell - r + 3, 137 | halfCell - r + 2, 138 | r * 2 - 6, 139 | r * 2 - 6, 140 | 90 * 2 + 20, 180 - 40 + 1); 141 | } 142 | 143 | @Override 144 | public Layer getLayer() { 145 | return Layer.MIDDLE; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/Labyrinth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import com.pavelfatin.game.behavior.ControlledBehavior; 21 | import com.pavelfatin.game.entity.Creature; 22 | import com.pavelfatin.game.entity.Dot; 23 | import com.pavelfatin.game.entity.Entity; 24 | 25 | import java.awt.*; 26 | import java.awt.geom.Rectangle2D; 27 | import java.util.ArrayList; 28 | import java.util.Collection; 29 | import java.util.Collections; 30 | import java.util.List; 31 | 32 | 33 | public class Labyrinth { 34 | private static final LayerComparator _layerComparator = new LayerComparator(); 35 | 36 | private int _cellSize; 37 | private int _stepSize; 38 | private Rectangle _bounds = new Rectangle(); 39 | private List _entities = new ArrayList(); 40 | private IntersectionFinder _intersectionFinder; 41 | private Entity _controlledEntity; 42 | 43 | 44 | Labyrinth(int cellSize, int stepSize) { 45 | _cellSize = cellSize; 46 | _stepSize = stepSize; 47 | } 48 | 49 | public void processTick() { 50 | for (Entity entity : _entities) { 51 | Direction decision = entity.getNextDirection(); 52 | 53 | if (!Direction.None.equals(decision)) { 54 | move(entity, decision); 55 | processIntersectionsFor(entity); 56 | } 57 | } 58 | 59 | removeEatenEntities(); 60 | } 61 | 62 | private void move(Entity entity, Direction decision) { 63 | Rectangle target = translate(entity.getPosition(), decision); 64 | entity.setPosition(target); 65 | _intersectionFinder.setRectangle(entity, target); 66 | } 67 | 68 | Rectangle translate(Rectangle rectangle, Direction direction) { 69 | return direction.translate(rectangle, _stepSize); 70 | } 71 | 72 | private void processIntersectionsFor(Entity entity) { 73 | for (Entity another : intersectedEntities(entity.getPosition())) { 74 | entity.processIntersection(another); 75 | another.processIntersection(entity); 76 | } 77 | } 78 | 79 | public void add(Entity entity, Point point) { 80 | Rectangle position = new Rectangle(point.x * _cellSize, 81 | point.y * _cellSize, 82 | _cellSize, 83 | _cellSize); 84 | entity.setSpace(new SpaceAdapter(this, entity)); 85 | entity.setPosition(position); 86 | 87 | _entities.add(entity); 88 | Collections.sort(_entities, _layerComparator); 89 | 90 | updateBounds(position); 91 | 92 | updateIntersectionFinder(); 93 | } 94 | 95 | private void updateBounds(Rectangle position) { 96 | Rectangle2D union = _bounds.createUnion(position); 97 | _bounds = union.getBounds(); 98 | } 99 | 100 | private void updateIntersectionFinder() { 101 | _intersectionFinder = new IntersectionFinder( 102 | _bounds.getSize(), new Dimension(_cellSize, _cellSize)); 103 | for (Entity each : _entities) { 104 | _intersectionFinder.add(each, each.getPosition()); 105 | } 106 | } 107 | 108 | private void removeEatenEntities() { 109 | Collection eatenEntities = new ArrayList(); 110 | 111 | for (Entity entity : _entities) { 112 | if (entity.isEaten()) { 113 | eatenEntities.add(entity); 114 | _intersectionFinder.remove(entity); 115 | } 116 | } 117 | 118 | _entities.removeAll(eatenEntities); 119 | } 120 | 121 | public Collection getAvailableDirections(Entity entity) { 122 | Collection result = new ArrayList(); 123 | 124 | for (Direction direction : Direction.MOVING_DIRECTIONS) { 125 | Rectangle target = translate(entity.getPosition(), direction); 126 | if (canBeAt(entity, target)) { 127 | result.add(direction); 128 | } 129 | } 130 | 131 | return result; 132 | } 133 | 134 | public boolean canBeAt(Entity entity, Rectangle rectangle) { 135 | if (_bounds.contains(rectangle)) { 136 | for (Entity another : intersectedEntities(rectangle)) { 137 | if (!entity.equals(another) 138 | && !entity.canPassThrough(another)) { 139 | return false; 140 | } 141 | } 142 | return true; 143 | } else { 144 | return false; 145 | } 146 | } 147 | 148 | public Direction getDirectionToward(Entity entity, Class type) { 149 | Compass compass = new Compass(this, entity, type); 150 | return compass.locate(); 151 | } 152 | 153 | public void setControlledEntity(Entity controlledEntity) { 154 | _controlledEntity = controlledEntity; 155 | } 156 | 157 | public void navigate(Direction direction) { 158 | ((ControlledBehavior) _controlledEntity.getBehavior()) 159 | .navigate(direction); 160 | } 161 | 162 | public boolean isWin() { 163 | return !isContains(Dot.class); 164 | } 165 | 166 | public boolean isLose() { 167 | return !isContains(Creature.class); 168 | } 169 | 170 | public int getScore() { 171 | return _controlledEntity.getCollectedScore(); 172 | } 173 | 174 | public boolean isIntersects(Rectangle position, Class type) { 175 | for (Entity entity : intersectedEntities(position)) { 176 | if (type.isAssignableFrom(entity.getClass())) { 177 | return true; 178 | } 179 | } 180 | return false; 181 | } 182 | 183 | private Collection intersectedEntities(Rectangle position) { 184 | return _intersectionFinder.intersection(position); 185 | } 186 | 187 | private boolean isContains(Class type) { 188 | return count(type) > 0; 189 | } 190 | 191 | private int count(Class type) { 192 | int result = 0; 193 | for (Entity object : _entities) { 194 | if (type.isAssignableFrom(object.getClass())) { 195 | result++; 196 | } 197 | } 198 | return result; 199 | } 200 | 201 | public Dimension getDimensions() { 202 | return _bounds.getSize(); 203 | } 204 | 205 | public void render(Graphics2D graphics) { 206 | for (Entity entity : _entities) { 207 | entity.render(graphics); 208 | } 209 | } 210 | } -------------------------------------------------------------------------------- /src/main/java/com/pavelfatin/game/IntersectionFinder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import java.awt.*; 21 | import java.util.*; 22 | 23 | 24 | public class IntersectionFinder { 25 | private Collection[][] _matrix; 26 | private int _width; 27 | private int _height; 28 | private int _logicalHeight; 29 | private int _logicalWidth; 30 | 31 | 32 | public IntersectionFinder(Dimension size, Dimension cellSize) { 33 | _width = size.width; 34 | _height = size.height; 35 | 36 | _logicalHeight = (int) Math.ceil((double) size.height / cellSize.height); 37 | _logicalWidth = (int) Math.ceil((double) size.width / cellSize.width); 38 | 39 | initMatrix(); 40 | } 41 | 42 | private void initMatrix() { 43 | _matrix = (Collection[][]) 44 | new Collection[_logicalWidth][_logicalHeight]; 45 | 46 | for (int y = 0; y < _logicalHeight; y++) { 47 | for (int x = 0; x < _logicalWidth; x++) { 48 | _matrix[x][y] = new ArrayList(); 49 | } 50 | } 51 | } 52 | 53 | public void add(T object, Rectangle rectangle) { 54 | if (outOfBounds(rectangle)) { 55 | throw new IllegalArgumentException(); 56 | } 57 | 58 | removeFromMatrix(object); 59 | 60 | addToMatrix(object, rectangle); 61 | } 62 | 63 | public void remove(T object) { 64 | if (matrixContains(object)) { 65 | removeFromMatrix(object); 66 | } else { 67 | throw new NoSuchElementException( 68 | "Can't remove unexisted object: " + object); 69 | } 70 | } 71 | 72 | private boolean removeFromMatrix(T object) { 73 | boolean removed = false; 74 | 75 | for (int y = 0; y < _logicalHeight; y++) { 76 | for (int x = 0; x < _logicalWidth; x++) { 77 | Collection entries 78 | = new ArrayList(_matrix[x][y]); 79 | for (Entry entry : entries) { 80 | if (object.equals(entry.object)) { 81 | _matrix[x][y].remove(entry); 82 | removed = true; 83 | } 84 | } 85 | } 86 | } 87 | 88 | return removed; 89 | } 90 | 91 | public Collection intersection(Rectangle rectangle) { 92 | Rectangle logical = toLogical(rectangle.intersection( 93 | new Rectangle(0, 0, _width, _height))); 94 | 95 | Set resultList = new HashSet(); 96 | 97 | for (int y = logical.y; y <= logical.y + logical.height; y++) { 98 | for (int x = logical.x; x <= logical.x + logical.width; x++) { 99 | for (Entry entry : _matrix[x][y]) { 100 | if (rectangle.intersects(entry.rectangle)) { 101 | resultList.add(entry.object); 102 | } 103 | } 104 | } 105 | } 106 | 107 | return resultList; 108 | } 109 | 110 | public void setRectangle(T object, Rectangle rectangle) { 111 | if (outOfBounds(rectangle)) { 112 | throw new IllegalArgumentException(); 113 | } 114 | 115 | if (removeFromMatrix(object)) { 116 | addToMatrix(object, rectangle); 117 | } else { 118 | throw new NoSuchElementException( 119 | "Can't set rectangle for unexisted object: " + object); 120 | } 121 | } 122 | 123 | private boolean matrixContains(T object) { 124 | return matrixObjects().contains(object); 125 | } 126 | 127 | private boolean outOfBounds(Rectangle rectangle) { 128 | return (rectangle.x + rectangle.width > _width) 129 | || (rectangle.y + rectangle.height > _height) 130 | || (rectangle.x < 0) 131 | || (rectangle.y < 0); 132 | } 133 | 134 | private Rectangle toLogical(Rectangle rectangle) { 135 | int left = rectangle.x; 136 | int right = rectangle.x + rectangle.width - 1; 137 | int bottom = rectangle.y; 138 | int top = bottom + rectangle.height - 1; 139 | 140 | int logicalLeft = (left * _logicalWidth / _width); 141 | int logicalRight = (right * _logicalWidth / _width); 142 | int logicalBottom = (bottom * _logicalHeight / _height); 143 | int logicalTop = (top * _logicalHeight / _height); 144 | 145 | return new Rectangle(logicalLeft, logicalBottom, 146 | logicalRight - logicalLeft, 147 | logicalTop - logicalBottom); 148 | } 149 | 150 | private void addToMatrix(T object, Rectangle rectangle) { 151 | Rectangle logical = toLogical(rectangle); 152 | 153 | for (int y = logical.y; y <= logical.y + logical.height; y++) { 154 | for (int x = logical.x; x <= logical.x + logical.width; x++) { 155 | _matrix[x][y].add(new Entry(object, rectangle)); 156 | } 157 | } 158 | } 159 | 160 | private Collection matrixEntries() { 161 | Collection result = new ArrayList(); 162 | 163 | for (int y = 0; y < _logicalHeight; y++) { 164 | for (int x = 0; x < _logicalWidth; x++) { 165 | for (Entry entry : _matrix[x][y]) { 166 | result.add(entry); 167 | } 168 | } 169 | } 170 | 171 | return result; 172 | } 173 | 174 | Collection matrixObjects() { 175 | Set result = new HashSet(); 176 | for (Entry entry : matrixEntries()) { 177 | result.add(entry.object); 178 | } 179 | return result; 180 | } 181 | 182 | Rectangle rectangleFor(T object) { 183 | Collection entries = matrixEntries(); 184 | for (Entry entry : entries) { 185 | if (object.equals(entry.object)) { 186 | return entry.rectangle; 187 | } 188 | } 189 | throw new NoSuchElementException(); 190 | } 191 | 192 | Collection logicalPointsFor(T object) { 193 | Collection result = new ArrayList(); 194 | 195 | for (int y = 0; y < _logicalHeight; y++) { 196 | for (int x = 0; x < _logicalWidth; x++) { 197 | for (Entry entry : _matrix[x][y]) { 198 | if (object.equals(entry.object)) { 199 | result.add(new Point(x, y)); 200 | } 201 | } 202 | } 203 | } 204 | 205 | return result; 206 | } 207 | 208 | 209 | private class Entry { 210 | T object; 211 | Rectangle rectangle; 212 | 213 | 214 | Entry(T object, Rectangle rectangle) { 215 | this.object = object; 216 | this.rectangle = rectangle; 217 | } 218 | } 219 | } -------------------------------------------------------------------------------- /src/test/java/com/pavelfatin/game/IntersectionFinderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 Pavel Fatin 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package com.pavelfatin.game; 19 | 20 | import junit.framework.TestCase; 21 | 22 | import java.awt.Dimension; 23 | import java.awt.Point; 24 | import java.awt.Rectangle; 25 | import java.util.Arrays; 26 | import java.util.Collection; 27 | import java.util.NoSuchElementException; 28 | 29 | 30 | public class IntersectionFinderTest extends TestCase { 31 | private static final Dimension SIZE = new Dimension(600, 400); 32 | private static final Dimension CELL_SIZE = new Dimension(22, 33); 33 | 34 | private IntersectionFinder _field; 35 | 36 | 37 | @Override 38 | protected void setUp() { 39 | _field = new IntersectionFinder(SIZE, CELL_SIZE); 40 | } 41 | 42 | public void testInitialState() { 43 | assertContentIs(); 44 | assertIntersectionIs(0, 0, SIZE.width, SIZE.height); 45 | } 46 | 47 | public void testContains() { 48 | assertNotContains(one()); 49 | assertNotContains(two()); 50 | 51 | add(one(), 100, 95, 18, 19); 52 | assertContains(one()); 53 | assertNotContains(two()); 54 | 55 | add(two(), 114, 94, 28, 9); 56 | assertContains(one()); 57 | assertContains(two()); 58 | } 59 | 60 | public void testAddition() { 61 | assertExcludes(one(), two()); 62 | 63 | add(one(), 0, 200, 11, 50); 64 | assertContentIs(one()); 65 | assertExcludes(two()); 66 | 67 | add(two(), 10, 300, 21, 60); 68 | assertContentIs(one(), two()); 69 | } 70 | 71 | public void testEqualObjectsAddition() { 72 | add(one(), 0, 200, 11, 50); 73 | add(two(), 0, 200, 11, 50); 74 | 75 | add(one(), 10, 100, 15, 60); 76 | 77 | assertContentIs(one(), two()); 78 | 79 | assertRectangleIs(one(), 10, 100, 15, 60); 80 | assertRectangleIs(two(), 0, 200, 11, 50); 81 | } 82 | 83 | public void testRemoval() { 84 | add(one(), 114, 94, 80, 19); 85 | add(two(), 211, 205, 70, 140); 86 | 87 | remove(one()); 88 | assertExcludes(one()); 89 | assertContentIs(two()); 90 | 91 | remove(two()); 92 | assertContentIs(); 93 | assertExcludes(one(), two()); 94 | } 95 | 96 | public void testTwoObjectsManagementWithSameRectangles() { 97 | add(one(), 400, 204, 80, 59); 98 | add(two(), 400, 204, 80, 59); 99 | 100 | assertContentIs(one(), two()); 101 | 102 | remove(one()); 103 | assertExcludes(one()); 104 | assertContentIs(two()); 105 | 106 | remove(two()); 107 | assertExcludes(one(), two()); 108 | assertContentIs(); 109 | } 110 | 111 | public void testGetRectangle() { 112 | add(one(), 400, 204, 80, 59); 113 | add(two(), 114, 94, 80, 19); 114 | 115 | assertRectangleIs(one(), 400, 204, 80, 59); 116 | assertRectangleIs(two(), 114, 94, 80, 19); 117 | } 118 | 119 | public void testMotion() { 120 | add(one(), 400, 204, 80, 59); 121 | add(two(), 114, 94, 80, 19); 122 | 123 | move(one(), 114, 94, 80, 19); 124 | 125 | assertRectangleIs(one(), 114, 94, 80, 19); 126 | assertRectangleIs(two(), 114, 94, 80, 19); 127 | } 128 | 129 | public void testUnexistedObjects() { 130 | Object unexisted = new Object(); 131 | 132 | try { 133 | move(unexisted, 0, 0, 0, 0); 134 | fail(); 135 | } catch (NoSuchElementException e) { 136 | } 137 | 138 | try { 139 | remove(unexisted); 140 | fail(); 141 | } catch (NoSuchElementException e) { 142 | } 143 | } 144 | 145 | public void testBoundConstraint() { 146 | add("foo", 0, 0, 1, 1); 147 | remove("foo"); 148 | 149 | add("foo", SIZE.width - 1, 0, 1, 1); 150 | remove("foo"); 151 | 152 | add("foo", SIZE.height - 1, 0, 1, 1); 153 | remove("foo"); 154 | 155 | try { 156 | add(one(), -1, 0, 1, 1); 157 | fail(); 158 | } catch (IllegalArgumentException e) { 159 | } 160 | 161 | try { 162 | add(one(), 0, -1, 1, 1); 163 | fail(); 164 | } catch (IllegalArgumentException e) { 165 | } 166 | 167 | try { 168 | add(one(), SIZE.width, 0, 1, 1); 169 | fail(); 170 | } catch (IllegalArgumentException e) { 171 | } 172 | 173 | try { 174 | add(one(), 0, SIZE.height, 1, 1); 175 | fail(); 176 | } catch (IllegalArgumentException e) { 177 | } 178 | 179 | assertExcludes(one()); 180 | assertContentIs(); 181 | 182 | add(one(), 0, 0, 1, 1); 183 | 184 | try { 185 | move(one(), SIZE.width, 0, 1, 1); 186 | fail(); 187 | } catch (IllegalArgumentException e) { 188 | } 189 | 190 | try { 191 | add(one(), SIZE.width, 0, 1, 1); 192 | fail(); 193 | } catch (IllegalArgumentException e) { 194 | } 195 | 196 | assertContentIs(one()); 197 | 198 | assertRectangleIs(one(), 0, 0, 1, 1); 199 | } 200 | 201 | public void testObjectFound() { 202 | assertIntersectionIs(0, 0, 50, 60); 203 | assertIntersectionIs(100, 200, 50, 60); 204 | 205 | add(one(), 10, 15, 10, 20); 206 | 207 | assertIntersectionIs(0, 0, 50, 60, one()); 208 | assertIntersectionIs(100, 200, 50, 60); 209 | } 210 | 211 | public void testObjectFoundAfterMoved() { 212 | add(one(), 10, 15, 10, 20); 213 | 214 | move(one(), 100, 150, 10, 20); 215 | 216 | assertIntersectionIs(100, 120, 50, 60, one()); 217 | assertIntersectionIs(0, 0, 50, 60); 218 | } 219 | 220 | public void testIntersection() { 221 | add(one(), 10, 15, 10, 20); 222 | 223 | assertIntersectionIs(18, 20, 10, 10, one()); 224 | assertIntersectionIs(19, 20, 10, 10, one()); 225 | 226 | assertIntersectionIs(0, 20, 10, 10); 227 | assertIntersectionIs(20, 20, 10, 10); 228 | 229 | assertIntersectionIs(15, 33, 50, 50, one()); 230 | assertIntersectionIs(15, 34, 50, 50, one()); 231 | 232 | assertIntersectionIs(15, 5, 50, 10); 233 | assertIntersectionIs(15, 35, 50, 50); 234 | } 235 | 236 | public void testExceptionalSearchDimensions() { 237 | add(one(), 10, 15, 10, 20); 238 | 239 | assertIntersectionIs(15, 0, 0, 50); 240 | assertIntersectionIs(0, 20, 50, 0); 241 | assertIntersectionIs(15, 20, 0, 0); 242 | 243 | assertIntersectionIs(15, 0, 1, 50, one()); 244 | assertIntersectionIs(0, 20, 50, 1, one()); 245 | assertIntersectionIs(15, 20, 1, 1, one()); 246 | } 247 | 248 | public void testExceptionalDimensions() { 249 | add(one(), 10, 15, 0, 20); 250 | assertContains(one()); 251 | assertIntersectionIs(0, 0, 50, 50); 252 | 253 | move(one(), 10, 15, 15, 0); 254 | assertIntersectionIs(0, 0, 50, 50); 255 | 256 | move(one(), 10, 15, 0, 0); 257 | assertIntersectionIs(0, 0, 50, 50); 258 | 259 | move(one(), 10, 15, 1, 20); 260 | assertIntersectionIs(0, 0, 50, 50, one()); 261 | 262 | move(one(), 10, 15, 15, 1); 263 | assertIntersectionIs(0, 0, 50, 50, one()); 264 | 265 | move(one(), 10, 15, 1, 1); 266 | assertIntersectionIs(0, 0, 50, 50, one()); 267 | } 268 | 269 | public void testNoSearchBound() { 270 | add(one(), 10, 15, 20, 20); 271 | 272 | assertIntersectionIs(-20, -15, 50, 50, one()); 273 | assertIntersectionIs(5, 5, SIZE.width + 20, SIZE.height + 50, one()); 274 | } 275 | 276 | public void testMultiplyObjectSearch() { 277 | int objectWidth = SIZE.width / 10; 278 | int objectHeight = SIZE.height / 10; 279 | 280 | 281 | add(one(), SIZE.width / 4 - objectWidth / 2, 282 | SIZE.height / 4 - objectHeight / 2, 283 | objectWidth, objectHeight); 284 | 285 | add(two(), SIZE.width / 2 - objectWidth / 2, 286 | SIZE.height / 2 - objectHeight / 2, 287 | objectWidth, objectHeight); 288 | 289 | add(three(), SIZE.width / 4 * 3 - objectWidth / 2, 290 | SIZE.height / 4 * 3 - objectHeight / 2, 291 | objectWidth, objectHeight); 292 | 293 | assertIntersectionIs(0, 0, 2 * SIZE.width / 3, 2 * SIZE.height / 3, 294 | one(), 295 | two()); 296 | 297 | assertIntersectionIs(SIZE.width - 2 * SIZE.width / 3, 298 | SIZE.height - 2 * SIZE.height / 3, 299 | 2 * SIZE.width / 3, 2 * SIZE.height / 3, two(), 300 | three()); 301 | 302 | assertIntersectionIs(0, 2 * SIZE.height / 5, SIZE.width, 5); 303 | assertIntersectionIs(2 * SIZE.width / 5, 0, 5, SIZE.height); 304 | } 305 | 306 | public void testSearchOnEdges() { 307 | add(one(), SIZE.width - 1, 50, 1, 1); 308 | add(two(), 50, SIZE.height - 1, 1, 1); 309 | add(three(), 0, 50, 1, 1); 310 | add("four", 50, 0, 1, 1); 311 | 312 | assertIntersectionIs(SIZE.width - 1, 50, 1, 1, one()); 313 | assertIntersectionIs(50, SIZE.height - 1, 1, 1, two()); 314 | assertIntersectionIs(0, 50, 1, 1, three()); 315 | assertIntersectionIs(50, 0, 1, 1, "four"); 316 | } 317 | 318 | public void testLogicalPositions() { 319 | _field = new IntersectionFinder( 320 | new Dimension(600, 400), new Dimension(30, 20)); 321 | 322 | add(one(), 0, 0, 30, 20); 323 | add(two(), 30, 0, 30, 20); 324 | add(three(), 0, 20, 30, 20); 325 | add("corner", SIZE.width - 30, SIZE.height - 20, 30, 20); 326 | 327 | assertLogicalPositionsIs(one(), new Point(0, 0)); 328 | assertLogicalPositionsIs(two(), new Point(1, 0)); 329 | assertLogicalPositionsIs(three(), new Point(0, 1)); 330 | assertLogicalPositionsIs("corner", 331 | new Point(SIZE.width / 30 - 1, 332 | SIZE.height / 20 - 1)); 333 | } 334 | 335 | public void testCompoundLogicalPosition() { 336 | _field = new IntersectionFinder( 337 | new Dimension(600, 400), new Dimension(30, 20)); 338 | 339 | add("big", 30, 40, 30 * 3, 20 * 2); 340 | 341 | assertLogicalPositionsIs("big", 342 | new Point(1, 2), 343 | new Point(1, 3), 344 | new Point(2, 2), 345 | new Point(2, 3), 346 | new Point(3, 2), 347 | new Point(3, 3)); 348 | } 349 | 350 | public void testIntermediateLogicalPositions() { 351 | _field = new IntersectionFinder( 352 | new Dimension(600, 400), new Dimension(10, 10)); 353 | 354 | add(one(), 0, 0, 1, 1); 355 | assertLogicalPositionsIs(one(), new Point(0, 0)); 356 | 357 | move(one(), 9, 9, 1, 1); 358 | assertLogicalPositionsIs(one(), new Point(0, 0)); 359 | 360 | move(one(), 10, 10, 1, 1); 361 | assertLogicalPositionsIs(one(), new Point(1, 1)); 362 | } 363 | 364 | public void testIntermediateLogicalDimensions() { 365 | _field = new IntersectionFinder( 366 | new Dimension(600, 400), new Dimension(10, 10)); 367 | 368 | add(one(), 0, 0, 10, 10); 369 | assertLogicalPositionsIs(one(), new Point(0, 0)); 370 | 371 | add(one(), 0, 0, 21, 10); 372 | assertLogicalPositionsIs(one(), 373 | new Point(0, 0), 374 | new Point(1, 0), 375 | new Point(2, 0)); 376 | 377 | add(one(), 0, 0, 11, 11); 378 | assertLogicalPositionsIs(one(), 379 | new Point(0, 0), 380 | new Point(1, 0), 381 | new Point(0, 1), 382 | new Point(1, 1)); 383 | } 384 | 385 | private Object one() { 386 | return "one"; 387 | } 388 | 389 | private Object two() { 390 | return "two"; 391 | } 392 | 393 | private Object three() { 394 | return "three"; 395 | } 396 | 397 | private Collection intersection(Rectangle rectangle) { 398 | return _field.intersection(rectangle); 399 | } 400 | 401 | private void assertLogicalPositionsIs(Object object, Point... points) { 402 | Collection foundPoints = _field.logicalPointsFor(object); 403 | 404 | assertEquals(points.length, foundPoints.size()); 405 | 406 | for (Point point : points) { 407 | assertTrue(foundPoints.contains(point)); 408 | } 409 | } 410 | 411 | private void assertIntersectionIs(int x, int y, int width, int height, 412 | Object... expected) { 413 | Collection foundObjects 414 | = intersection(new Rectangle(x, y, width, height)); 415 | assertEquals(expected.length, foundObjects.size()); 416 | assertTrue(foundObjects.containsAll(Arrays.asList(expected))); 417 | } 418 | 419 | private void add(Object object, int x, int y, int width, int height) { 420 | _field.add(object, new Rectangle(x, y, width, height)); 421 | } 422 | 423 | private void remove(Object object) { 424 | _field.remove(object); 425 | } 426 | 427 | private void move(Object object, int x, int y, int width, int height) { 428 | _field.setRectangle(object, new Rectangle(x, y, width, height)); 429 | } 430 | 431 | private void assertSizeIs(int size) { 432 | assertEquals(size, _field.matrixObjects().size()); 433 | } 434 | 435 | private void assertContentIs(Object... objects) { 436 | assertSizeIs(objects.length); 437 | 438 | for (Object object : objects) { 439 | assertContains(object); 440 | } 441 | } 442 | 443 | private void assertExcludes(Object... objects) { 444 | for (Object object : objects) { 445 | assertNotContains(object); 446 | } 447 | } 448 | 449 | private void assertContains(Object object) { 450 | assertTrue(_field.matrixObjects().contains(object)); 451 | } 452 | 453 | private void assertNotContains(Object object) { 454 | assertFalse(_field.matrixObjects().contains(object)); 455 | } 456 | 457 | private void assertRectangleIs(Object object, int x, int y, 458 | int width, int height) { 459 | assertEquals(new Rectangle(x, y, width, height), 460 | _field.rectangleFor(object)); 461 | } 462 | } 463 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------