Hi I was wondering on how to make an enemy shoot in a spiral way around the screen.
BasicEnemy(float tempX, float tempY, int tempXdir, int tempYdir, int tempSize, float tempSpeed, float tempDamage)
Thats the constructor for a square enemy that just moves in a direction until it leaves the screen so how would I be able to make an enemy shoot out a bunch of these types of enemies in a spiral way out. I was thinking on using map but I’m not sure how do this. Please help me out if you can or point me in the right direction.
If i didn‘t get you wrong, you probably just want to set tempX and tempY to the position of your main enemy. And set tempDirX and tempDirY to sin(angle) and cos(angle) respectively.
Then just use 0 as the first angle and increase it every time a new enemy is shot. The spiral pattern is created automatically because of the delay between shots.
I’m a little confused on how to get the cos and sin angles to constantly change after each shot yes you have the right idea as the Xdir and Ydir are the directions they go at a constant speed. I have no idea how to implement the cos and sin angles for each shot but I do know how I could set a delay after each shot. If you could give me an idea for the cos and sin angles variables.
The angle variable is just one. At the first shot it‘s 0, then it increments by some value (let‘s say 1), so the next enemy is shot at angle 1, then increment again, and so on and so forth.
Something like this :
Enemy e;
void setup() {
size(300,600);
e = new Enemy(150,150);
}
void draw() {
println(frameCount);
background(255);
if (frameCount%20==0)
e.shoot();
println("shot");
e.moveBullets();
e.display();
//e.posX += 0.1;
}
class Bullet {
//this is the shot enemy, but can‘t call both enemy...
float posX, posY;
float dirX;
float dirY;
Bullet (float x, float y, float dx, float dy) {
posX = x;
posY = y;
dirX = dx;
dirY = dy;
}
void display() {
ellipse(posX, posY, 3, 3);
}
void move() {
posX += dirX;
posY += dirY;
}
}
class Enemy {
final float BULLET_SPEED = 0.8;
final float ANGLE_INCREMENT = PI/2;
//2PI would only shoot down, PI is up down,
//halfPi is in 4 dirs and so on
//you can also just use some random number like 0.3256
//but try keeping it within 0-2PI
float posX, posY;
Bullets[] bullets; //might want to use a list
Enemy (float x, float y) {
posX = x;
posY = y;
bullets = new Bullets[0];
}
void display() {
ellipse(posX, posY, 10, 10);
for (int i = 0; i < bullets.length; i++) bullets[i].display();
}
void shoot() {
bullets = append(bullets, new Bullet(posX, posY, sin(bullets.length*ANGLE_INCREMENT)*BULLET_SPEED, cos(bullets.length*ANGLE_INCREMENT)*BULLET_SPEED));
}
void moveBullets() {
for (int i = 0; i < bullets.length; i++) {
bullets[i].move();
}
}
}
Oh… right, i though there was something about angle 0 starting at 3 o‘clock recently well, as long as it‘s not going to cause any problems i‘ll just use it how it works
Thank you all for the help it’s great to know that people are very kind and helpful here, I have a bit of an understanding to how I could use trigonometry for this because I only remember SOH CAH TOA as a memory saying in school and it’s only for right triangles.