I am trying to make code for knock back when the bullet hits a enemy
My bullet curves if my character moves up and down while shooting
Here is what i have so far for knockBack
// ENEMY CODE
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class SmartGrunt here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SmartGrunt extends Enemy
{
GreenfootSound shootSFX = new GreenfootSound("enemyshoot.mp3");
private int health = 10;
private int knockBackDown = 200;
private int knockBackUp = 200;
/**
* Act - do whatever the Grunt wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
followPlayer();
shoot();
knockBack();
checkDeath();
}
public void health(int healthLost){
health = health + healthLost;
}
public int getHealth(){
return health;
}
//follows the player once the player is in the dist (500)
public void followPlayer(){
int dist = 500;
Actor closest = null;
//if there is a Hero in the given distance
if(!getObjectsInRange(dist, Hero.class).isEmpty()){
for (Object obj: getObjectsInRange(dist, Hero.class)) {
Actor Hero = (Actor) obj;
int heroDist = (int) Math.hypot(Hero.getX() - getX(), Hero.getY() - getY());
if (closest == null || heroDist < dist) {
closest = Hero;
dist = heroDist;
}
}
turnTowards(closest.getX(), closest.getY());
move(2);
}
}
//shoots the player
public void shoot(){
int dist = 1000;
EnemyBullet a = new EnemyBullet();
double hypLength;
double adjLength;
Double d_radAngle; //starting point
double radAngleUnbox;
int i_radAngle;
Integer degAngle;
if(!getObjectsInRange(dist, Hero.class).isEmpty()){
for (Object obj: getObjectsInRange(dist, Hero.class)) {
Actor Hero = (Actor) obj;
hypLength = (double)Math.sqrt(Math.pow((Hero.getX() - getX()), 2) + Math.pow((Hero.getY() - getY()), 2));
adjLength = (double)Hero.getX() - getX();
d_radAngle = Math.acos(adjLength/hypLength) * 180/Math.PI; //starting point
radAngleUnbox = d_radAngle.doubleValue(); //unbox
i_radAngle = (int) radAngleUnbox;//cast
degAngle = Integer.valueOf(i_radAngle);//box
//shoots random at random intervals
if (Greenfoot.getRandomNumber(500) < 10){
shootSFX.play();
getWorld().addObject(a, getX(), getY());
if (Hero.getY() <= getY()){
a.setRotation(-degAngle);
}
else {
a.setRotation(degAngle);
}
}
}
}
}
public void knockBack(){
//HeroBullet h = ((HeroBullet) getWorld().getObjects(HeroBullet.class).get(0));
Actor d = getOneIntersectingObject(HeroBullet.class);
// diangonal distance between bullet and enemy
//double hypBtwnBulletNEnemy = Math.sqrt(Math.pow((h.getX() - getX()) ,2) + Math.pow((h.getY() - getY()), 2));
if(d!=null){
knockBackUp++;
knockBackDown++;
if(getX() < 540 && getY() >360){ // bottom left
setLocation (getX() - knockBackDown, getY() + knockBackUp);
}
if (getX() < 540 && getY() < 360){ // top left
setLocation (getX() - knockBackDown, getY() - knockBackUp);
}
if (getX() > 540 && getY() > 360){ // bottom right
setLocation (getX() + knockBackDown, getY() + knockBackUp);
}
if (getX() > 540 && getY() < 360){ // Top right
setLocation (getX() + knockBackDown, getY() - knockBackUp);
}
}
}
public void checkDeath() {
Actor d = getOneIntersectingObject(HeroBullet.class);
if (d!=null) {
int healthLost = getWorld().getObjects(Hero.class).get(0).getDamage();
health(healthLost);
getWorld().removeObject(getOneIntersectingObject(HeroBullet.class));
if (getHealth() == 0){
getWorld().removeObject(this);
}
}
}
}// BULLET OF MAIN CHARACTER
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class HeroBullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class HeroBullet extends Bullet
{
/**
* Act - do whatever the HeroBullet wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int damage = -1;
private boolean notAlreadySet = true, notAlreadySet2 = true;
String key = "";
int whichDirection = 0;
double accelR = 2.5, accelL = -2.5, accelU = -2.5, accelD = 2.5;
int rightValue = 0;
int leftValue = 0;
int upValue = 0;
int downValue = 0;
public void act() {
getExactKey();
setBoolean();
setDamage();
curve();
move(10);
remove();
}
// remove if the bullet hits an ostacles
// remove if the bullet is at the edge of the world
public void remove(){
Actor d = getOneIntersectingObject(Enemy.class);
Actor f = getOneIntersectingObject(Obstacle.class);
if (f!=null || atWorldEdge()) { //if the bullet is touching an enemy or an obstacle
getWorld().removeObject(this); //removes this specific instance of bullet
}
}
public boolean atWorldEdge(){
if(getX() < 10 || getX() > getWorld().getWidth() - 10)
return true;
if(getY() < 10 || getY() > getWorld().getHeight() - 10)
return true;
else
return false;
}
public void curve() {
Hero h = ((Hero) getWorld().getObjects(Hero.class).get(0));
if (whichDirection == 1 && !(key.equals("left"))) {
setLocation(getX()+rightValue, getY());
rightValue+=accelR;
accelR-=0.5;
if (accelR < 0) {
accelR = 0;
}
} else if (whichDirection == 2 && !(key.equals("right"))) {
setLocation(getX()-leftValue, getY());
leftValue-=accelL;
accelL+=0.5;
if (accelL > 0) {
accelL = 0;
}
} else if (whichDirection == 3 && !(key.equals("down"))) {
//System.out.println("in" + " " + whichDirection + " " + key);
setLocation(getX(), getY()-upValue);
upValue-=accelU;
accelU+=0.5;
if (accelU > 0) {
accelU = 0;
}
} else if (whichDirection == 4 && !(key.equals("up"))) {
setLocation(getX(), getY()+downValue);
downValue+=accelD;
accelD-=0.5;
if (accelD < 0) {
accelD = 0;
}
} else {
}
//rightValue = 0;
//leftValue = 0;
}
public void setBoolean() {
Hero h = ((Hero) getWorld().getObjects(Hero.class).get(0));
if (notAlreadySet) {
if (h.movingDirection(1)) {
whichDirection = 1;
} else if (h.movingDirection(2)) {
whichDirection = 2;
} else if (h.movingDirection(3)){
whichDirection = 3;
} else if (h.movingDirection(4)) {
whichDirection = 4;
}
notAlreadySet = false;
}
}
public void getExactKey() {
Hero h = ((Hero) getWorld().getObjects(Hero.class).get(0));
if (notAlreadySet2) {
key = h.returnV();//System.out.println(key);
notAlreadySet2 = false;
}
}
//sets the damage according to the damge set in hero
public void setDamage(){
damage = getWorld().getObjects(Hero.class).get(0).getDamage();
}
}