Skip to content

Commit

Permalink
commiting assign3
Browse files Browse the repository at this point in the history
  • Loading branch information
atararazin committed May 19, 2019
0 parents commit f051d2a
Show file tree
Hide file tree
Showing 18 changed files with 1,301 additions and 0 deletions.
7 changes: 7 additions & 0 deletions oopass3/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="/ass5/biuoop-1.4.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions oopass3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions oopass3/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>oopass3</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions oopass3/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
9 changes: 9 additions & 0 deletions oopass3/src/ArkMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

public class ArkMain {
public static void main(String[] args) {
System.out.println("in current");
Game game = new Game();
game.initialize();
game.run();
}
}
195 changes: 195 additions & 0 deletions oopass3/src/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import java.awt.Color;
import java.util.List;

import biuoop.DrawSurface;

/**
* The ball class defines a filled circle of a given color that knows the bounds of its movements.
* The ball is able to handle the physics of hitting walls on its given boundaries,
* and may print itself.
*
* @author Benjy Berkowicz & Atara Razin
*/
public class Ball implements Sprite {
private Point center;
private int radius;
private Color color;
// The velocity is initialized to 0,0 by default (the user doesn't need to set them)
private Velocity ballVelocity = new Velocity(0, 0);
private GameEnvironment game;

// Constructors
/**
* The main constructor for the ball - takes all possible parameters including top/bottom boundary
* points for the ball to bounce within. Creates a new ball with given characteristics.
*
* @param x the x starting position of the ball.
* @param y the y starting position of the ball.
* @param r the radius of the ball.
* @param color the color that fills the ball.
* @param topBound the top Point of the 'rectangle' within which the ball bounces.
* @param bottomBound the bottom Point of the 'rectangle' within which the ball bounces.
*/
public Ball(int x, int y, int r, java.awt.Color color, GameEnvironment game) {
this.radius = r;
//Creates a new 'Point' for the center of the ball
this.center = new Point(x, y);
this.color = color;
this.game = game;
}

/**
* A more basic constructor than the one above, takes a point - but
* takes no bounds for the ball - useful for constructing a static ball.
*
* @param center the Point where the ball will be created.
* @param r the radius of the ball.
* @param color the color of the ball.
*/
public Ball(Point center, int r, java.awt.Color color) {
this.radius = r;
//Creates a new 'Point' for the center of the ball
this.center = center;
this.color = color;
}

// Basic Accessors

/**
* getX returns the x coordinate of the center of the ball.
* @return the x coordinate of the center of the ball.
*/
public int getX() {
//Casting to int is done because Point's getX function returns a double.
return (int) this.center.getX();
}

/**
* getY returns the y coordinate of the center of the ball.
* @return the y coordinate of the center of the ball.
*/
public int getY() {
//Casting to int is done because Point's getY function returns a double.
return (int) this.center.getY();
}

/**
* getSize returns the radius of the ball.
* @return the radius of the ball
*/
public int getSize() {
return this.radius;
}

/**
* getColor returns the color of the ball as a java color object.
* @return the color of the ball.
*/
public java.awt.Color getColor() {
return this.color;
}

/**
* getVelocity returns the current velocity of the ball.
* @return the velocity of the ball as a Velocity object.
*/
public Velocity getVelocity() {
return this.ballVelocity;
}

// General Methods
/**
* Adds itself to the list of sprites in the game.
*
* @param g (the Game we are working with).
*/
public void addToGame(Game g) {
g.addSprite(this);
}

/**
* moveOneStep handles the movement of a ball for one 'step' according to its velocity and the bounds
* of its movement. When the ball is detected as hitting or passing a given bound, its x or y velocity
* is reversed - and its x and y position are reset to be within the bounds of the field.
*/
public void timePassed() {
//changed the name from moveonestep to timepassed
// A new center, the desired location of the ball, is generated.
Point newCenter = this.getVelocity().applyToPoint(this.center);
//we create a new line, which is the distance between the new and old centers.
Line trajectory = new Line(this.center, newCenter);
//gets the info of the next hit of the trajectory
CollisionInfo nextHit = game.getClosestCollision(trajectory);

//if the next hit is not the new center, set the new center as newCenter.
if (nextHit == null) {
this.center = newCenter;
}
else {
/*
* We first adjust the X/Y position of the ball as needed. This is done by detecting which
* side of the collision rectangle has been hit and moving the center of the ball to a point just 'touching'
* the collision rectangle, but not inside it.
*/

Point hitSpot = nextHit.collisionPoint();
List<Line> collisionLines = nextHit.collisionObject().getCollisionRectangle().componentLines();

// Case 1: We hit the 'horizontal top' line of the rectangle, therefore adjust the Y coordinate.
Line oneLine = collisionLines.get(0);
if (oneLine.isOnLine(hitSpot)) {
this.center.setY(hitSpot.getY() - radius);
}

// Case 2: We hit the 'horizontal bottom' line of the rectangle, therefore adjust the Y coordinate.
oneLine = collisionLines.get(1);
if (oneLine.isOnLine(hitSpot)) {
this.center.setY(hitSpot.getY() + radius);
}

// Case 3: We hit the 'vertical left' line of the rectangle, therefore adjust the Y coordinate.
oneLine = collisionLines.get(2);
if (oneLine.isOnLine(hitSpot)) {
System.out.println("hit a left vert");
this.center.setX(hitSpot.getX() - radius);
}

// Case 3: We hit the 'vertical right' line of the rectangle, therefore adjust the Y coordinate.
oneLine = collisionLines.get(3);
if (oneLine.isOnLine(hitSpot)) {
System.out.println("hit a right vert");
this.center.setX(hitSpot.getX() + radius);
}

this.ballVelocity = nextHit.collisionObject().hit(hitSpot, this.ballVelocity);
}
}

/**
* Draws the ball on the provided DrawSurface. Generally this method is called after moveOneStep.
* @param surface the surface for the ball to be drawn on.
*/
public void drawOn(DrawSurface surface) {
surface.setColor(this.color);
// We call our accessors to find the dimensions needed for printing.
surface.fillCircle(this.getX(), this.getY(), this.radius);
}

/**
* Used to set the velocity of the ball given an independantly created Velocity object.
* @param v the new velocity of the ball.
*/
public void setVelocity(Velocity v) {
this.ballVelocity = v;
}

/**
* Used to set the velocity of the ball using a delta X and delta Y parameters.
* @param dx the change in x position of the ball per step movement
* @param dy the change in y position of the ball per step movement
*/
public void setVelocity(double dx, double dy) {
this.ballVelocity.setDeltaX(dx);
this.ballVelocity.setDeltaY(dy);
}
}
93 changes: 93 additions & 0 deletions oopass3/src/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

import biuoop.DrawSurface;

public class Block implements Collidable, Sprite {

private Rectangle rect;
private int hitPoints;
private java.awt.Color color;

//constructor
public Block(Rectangle rect, int hitPoints, java.awt.Color color) {
this.rect = rect;
this.color = color;
this.hitPoints = hitPoints;
}

// Return the "collision shape" of the object.
public Rectangle getCollisionRectangle() {
return this.rect;
}

// Notify the object that we collided with it at collisionPoint with
// a given velocity.
// The return is the new velocity expected after the hit (based on
// the force the object inflicted on us).
public Velocity hit(Point collisionPoint, Velocity currentVelocity) {
this.hitPoints = Math.max(this.hitPoints - 1, 0);
Line oneLine;

// Case 1: We hit the 'horizontal top' line of the rectangle, therefore we reverse Y velocity.
oneLine = this.rect.componentLines().get(0);
if (oneLine.isOnLine(collisionPoint)) {
currentVelocity.setDeltaY(-1 * Math.abs(currentVelocity.getDeltaY()));
System.out.println("hit a horz top");
}

// Case 2: We hit the 'horizontal bottom' line of the rectangle, therefore we reverse Y velocity.
oneLine = this.rect.componentLines().get(1);
if (oneLine.isOnLine(collisionPoint)) {
currentVelocity.setDeltaY(Math.abs(currentVelocity.getDeltaY()));
System.out.println("hit a horz bot");
}

// Case 3: We hit the 'left verticle' line of the rectangle, therefore we reverse X velocity.
oneLine = this.rect.componentLines().get(2);
if (oneLine.isOnLine(collisionPoint)) {
currentVelocity.setDeltaX(-1 * Math.abs(currentVelocity.getDeltaX()));
System.out.println("hit a left verticle");
}

// Case 4: We hit the 'right verticle' line of the rectangle, therefore we reverse X velocity.
oneLine = this.rect.componentLines().get(3);
if (oneLine.isOnLine(collisionPoint)) {
currentVelocity.setDeltaX(Math.abs(currentVelocity.getDeltaX()));
System.out.println("hit a right verticle");
}
return currentVelocity;
}

public void timePassed() {

}

public void drawOn(DrawSurface surface) {
Point corner = this.rect.getUpperLeft();
int rectHeight = (int) this.rect.getHeight();
int rectWidth = (int) this.rect.getWidth();
int x = (int) corner.getX();
int y = (int) corner.getY();
String text;

if (this.hitPoints > 0) {
text = Integer.toString(this.hitPoints);
}
else {
text = "X";
}
surface.setColor(java.awt.Color.BLACK);
surface.fillRectangle(x, y, rectWidth, rectHeight);

surface.setColor(this.color);
// We call our accessors to find the dimensions needed for printing.
surface.fillRectangle(x + 2, y + 2, rectWidth - 4, rectHeight - 4);
surface.setColor(java.awt.Color.WHITE);
surface.drawText(x + (rectWidth / 2), y + (rectHeight/ 2), text, 14);
}

//implements the addtogame method in the interface sprite
public void addToGame(Game g) {
g.addSprite(this);
g.addCollidable(this);
}
}
12 changes: 12 additions & 0 deletions oopass3/src/Collidable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

public interface Collidable {
// Return the "collision shape" of the object.
Rectangle getCollisionRectangle();

// Notify the object that we collided with it at collisionPoint with
// a given velocity.
// The return is the new velocity expected after the hit (based on
// the force the object inflicted on us).
Velocity hit(Point collisionPoint, Velocity currentVelocity);
}

37 changes: 37 additions & 0 deletions oopass3/src/CollisionInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This class is the way of accessing the information about the collision. Using this class, we can
* find out where the collision occured and with what object.
*
* @author Benjy Berkowicz & Atara Razin
*/
public class CollisionInfo {
//members
private Point hitPoint;
private Collidable hitObject;

/**
* constructor.
* @param hitPoint (the point where the hit occured).
* @param hitObject (the object with which the collision occurred).
*/
public CollisionInfo(Point hitPoint, Collidable hitObject) {
this.hitObject = hitObject;
this.hitPoint = hitPoint;
}

/**
* Returns the point at which the collision occurs.
* @return hitPoint (the collision point).
*/
public Point collisionPoint() {
return this.hitPoint;
}

/**
* Returns the the collidable object involved in the collision.
* @return hitObject (the collision object).
*/
public Collidable collisionObject() {
return this.hitObject;
}
}
Loading

0 comments on commit f051d2a

Please sign in to comment.