// по нажатию создается пуля и помещается в пул, затем берется из него if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) { Bullet item = bulletPool.obtain(); item.init(item.getX(), item.getY(), player.getsGun().getRotation()); activeBullets.add(item); } // обновить пули, если за пределами экрана - вернуть в пул for (Bullet bullet : activeBullets) { bullet.update(dt); if (bullet.outOfScreen) { activeBullets.removeValue(bullet, true); bulletPool.free(bullet); } } As I understand it, a bullet is brought into the pool with the saved init(item.getX(), item.getY(), player.getsGun().getRotation()); that is, a bullet in a bullet has a specific X, Y coordinate, as well as an immutable Rotation value - each bullet from the pool will fly with the rotation with which it was entered into the pool. And now the question is how to change the rotation values in the pool? I need a bullet from the pool to fly to the point where the tower is turned (and it rotates constantly).
Update: Mm, the fact is that the bullet does not know where to fly. Here is the bullet code without using a pool.
public class Bullet extends BasicObject { private float lifeTime; private float lifeTimer; private boolean remove; public Bullet(float x, float y, float radians) { this.x = x; this.y = y+50; this.radians = radians; float speed = 350; dx = MathUtils.cos(radians) * speed; dy = MathUtils.sin(radians) * speed; width = height = 2; rectangle = new Rectangle(x, y, width, height); lifeTimer = 0; lifeTime = 2; } public boolean shouldRemove() { return remove; } public void update(float dt) { x += dx * dt; y += dy * dt; rectangle.x = x; rectangle.y = y; lifeTimer += dt; if (lifeTimer > lifeTime) { remove = true; } } public void draw(ShapeRenderer sr) { sr.setColor(1, 1, 1, 1); sr.begin(ShapeRenderer.ShapeType.Filled); sr.circle(x - width / 2, y - height / 2, width / 2); sr.end(); } } But the Player class:
public class Player extends BasicObject { private ArrayList<Bullet> bullets; private final int MAX_BULLETS = 20; private Texture tTower; private Texture tGun; private Sprite sTower; private Sprite sGun; private float curAngle; private Rectangle rGun; radians = 3.1415f / 2; public Player(ArrayList<Bullet> bullets) { this.bullets = bullets; x = MyMainClass.WIDTH / 2; y = 0; // tower rectangle = new Rectangle(); rectangle.x = x - 64/2; rectangle.y = y; rectangle.width = 64; rectangle.height = 64; rGun = new Rectangle(); rGun.x = x - 64/2; rGun.y = y + 10; rGun.width = 64; rGun.height = 64; tTower = new Texture("TOWER.png"); sTower = new Sprite(tTower); tGun = new Texture("TOWER_GUN.png"); sGun = new Sprite(tGun); sTower.setPosition(rectangle.x, rectangle.y); sGun.setPosition(rGun.x, rGun.y); sGun.setOrigin(sGun.getWidth() / 2, sGun.getHeight() - sGun.getHeight()); } public void shoot() { if (bullets.size() == MAX_BULLETS) return; bullets.add(new Bullet(x, y, radians)); } public float getX(){return x;} public float getY(){return y;} public void update(float dt) { if (Gdx.input.isTouched()) { // тут код по нахождению угла поворота башни и ограничении ее движения ....... ....... // set rotation radians = (MathUtils.PI / 180 * sGun.getRotation())+ MathUtils.PI/2; } public void draw(SpriteBatch sb) { sb.setColor(1, 1, 1, 1); sb.begin(); sTower.draw(sb); sGun.draw(sb); sb.end(); } } In PlayState I write if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) { player.shoot(); } if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) { player.shoot(); } , and as a result the bullet flies where the gun is turned. And everything would be fine, only the bullet is constantly being re-created and then deleted, and there are enough such objects to be created and besides the bullet, I’m trying to make a pool.
bullet.setRotation(double angle)? - Nofate ♦BulletandPlayerclasses without using a pool - Krem Soda