| 1 | /* |
| 2 | * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. |
| 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 <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "entityCreator.h" |
| 19 | |
| 20 | #include <queue> |
| 21 | |
| 22 | #include "entityManager.h" |
| 23 | #include "input/inputManager.h" |
| 24 | |
| 25 | #include "Entities/Ball.h" |
| 26 | #include "Entities/Polygon.h" |
| 27 | #include "graphics/colors.h" |
| 28 | |
| 29 | |
| 30 | /// ***** Private Variables ***** |
| 31 | |
| 32 | typedef std::queue<Ball*> queBall; |
| 33 | |
| 34 | queBall startBalls; |
| 35 | queBall mouseBalls; |
| 36 | |
| 37 | /// ***** Private Method Headers ***** |
| 38 | |
| 39 | Ball* addBall(const Vector2& pos, |
| 40 | float radius, |
| 41 | const float* color, |
| 42 | queBall& que); |
| 43 | |
| 44 | void removeBall(queBall& que); |
| 45 | void removeAllBalls(queBall& que); |
| 46 | |
| 47 | /// ***** Initializers/Cleaners ***** |
| 48 | |
| 49 | void creator::init() |
| 50 | { |
| 51 | Ball* ball; |
| 52 | float startMass = 5; |
| 53 | |
| 54 | ball = addBall(Vector2(50, 50), 20, cWhite, startBalls); |
| 55 | ball->mass = startMass; |
| 56 | |
| 57 | ball = addBall(Vector2(150, 50), 20, cGrey, startBalls); |
| 58 | ball->mass = startMass; |
| 59 | |
| 60 | ball = addBall(Vector2(50, 100), 20, cRed, startBalls); |
| 61 | ball->mass = startMass; |
| 62 | |
| 63 | ball = addBall(Vector2(100, 100), 20, cGreen, startBalls); |
| 64 | ball->mass = startMass; |
| 65 | |
| 66 | ball = addBall(Vector2(150, 100), 20, cBlue, startBalls); |
| 67 | ball->mass = startMass; |
| 68 | |
| 69 | ball = addBall(Vector2(50, 150), 20, cYellow, startBalls); |
| 70 | ball->mass = startMass; |
| 71 | |
| 72 | ball = addBall(Vector2(100, 150), 20, cMagenta, startBalls); |
| 73 | ball->mass = startMass; |
| 74 | |
| 75 | ball = addBall(Vector2(150, 150), 20, cCyan, startBalls); |
| 76 | ball->mass = startMass; |
| 77 | |
| 78 | for( int i = 0; i<50; i++) |
| 79 | { |
| 80 | addBall(Vector2(200+i*2, 200+i*2), 10, cCyan); |
| 81 | } |
| 82 | |
| 83 | // HACK |
| 84 | // add a polygon into the mix (currently not cleaned up) |
| 85 | vector<Vector2> points; |
| 86 | points.push_back(Vector2(500,500)); |
| 87 | points.push_back(Vector2(300,500)); |
| 88 | points.push_back(Vector2(500,300)); |
| 89 | |
| 90 | manager::add(new Polygon(points, cRed)); |
| 91 | } |
| 92 | void creator::clean() |
| 93 | { |
| 94 | removeAllBalls(startBalls); |
| 95 | removeAllBalls(mouseBalls); |
| 96 | } |
| 97 | |
| 98 | /// ***** Public Methods ***** |
| 99 | |
| 100 | void creator::addBall(const Vector2& pos, float radius, const float* color) |
| 101 | { |
| 102 | addBall(pos, radius, color, mouseBalls); |
| 103 | } |
| 104 | void creator::removeBall() |
| 105 | { |
| 106 | removeBall(mouseBalls); |
| 107 | } |
| 108 | void creator::removeAllBalls() |
| 109 | { |
| 110 | removeAllBalls(mouseBalls); |
| 111 | } |
| 112 | |
| 113 | void creator::handleInput() |
| 114 | { |
| 115 | if(input::mouseLeft()) |
| 116 | addBall(input::mousePosition(), 10, cCyan); |
| 117 | |
| 118 | if(input::mouseRight() && ! mouseBalls.empty()) |
| 119 | removeBall(); |
| 120 | } |
| 121 | |
| 122 | /// ***** Private Methods ***** |
| 123 | |
| 124 | Ball* addBall(const Vector2& pos, |
| 125 | float radius, |
| 126 | const float* color, |
| 127 | queBall& que) |
| 128 | { |
| 129 | Ball* ball = new Ball(pos, radius, color); |
| 130 | |
| 131 | que.push(ball); |
| 132 | manager::add(ball); |
| 133 | |
| 134 | return ball; |
| 135 | } |
| 136 | |
| 137 | void removeBall(queBall& que) |
| 138 | { |
| 139 | Ball* ball = que.front(); |
| 140 | |
| 141 | manager::remove(ball); |
| 142 | que.pop(); |
| 143 | |
| 144 | delete ball; |
| 145 | } |
| 146 | void removeAllBalls(queBall& que) |
| 147 | { |
| 148 | while(! que.empty() ) |
| 149 | { |
| 150 | Ball* ball = que.front(); |
| 151 | que.pop(); |
| 152 | manager::remove(ball); |
| 153 | |
| 154 | delete ball; |
| 155 | } |
| 156 | } |