So I'm making a snowball fight game and i want the character to throw a snowball when the mouse is clicked. I want the snowball to add itself where the character is and then go in the direction of the mouse. Also i'd like the snowball to go the distance of the snowball (add force according to where the mouse click occurs) and have a max as to how far the mouse can move from the character. how can i do this (sorry i realize that this is a lot).
here is my code (i use the smooth mover and vector class from greenfoot)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
* Write a description of class Snowball here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Snowball extends SmoothMover
{
private static final Vector GRAVITY = new Vector(90 , 0.2);
private double velocity;
int x=0;
public Snowball()
{
getImage().scale(13,13);
int angle = Greenfoot.getRandomNumber(41) + 250;
double force = Greenfoot.getRandomNumber(1000)/100.0 + 5;
addForce( new Vector( angle , force ) );
}
public void act()
{
move();
addForce(GRAVITY);
turn(10);
if ( atWorldEdges()||collision() )
{
explosion();
getWorld().removeObject(this);
}
//if ( atPeak() )
//{
// explosion();
// }
}
public boolean collision(){
Actor wall = (Actor)getOneIntersectingObject(OG_Obstacle.class);
Actor p1 = (Actor)getOneIntersectingObject(Fighter.class);
Actor p2 = (Actor)getOneIntersectingObject(Fighter2.class);
if(wall!=null){
return true;
}
if(p1!=null){
return true;
}
if(p2!=null){
return true;
}
return false;
}
public boolean atWorldEdges()
{
if ( getExactX() < 5 )
{
return true;
}
if ( getExactY() < 5 )
{
return true;
}
if ( getExactX() > getWorld().getWidth() - 5 )
{
return true;
}
if ( getExactY() > getWorld().getHeight() - 5 )
{
return true;
}
return false;
}
public boolean atPeak()
{
int angle = getMovement().getDirection();
return angle > 0 && angle < 180;
}
public void explosion()
{
for ( int i = 0; i < 50; i++ )
{
getWorld().addObject( new Debris(), getX(), getY() );
}
}
}