@Override protected void process(Entity e) { final Physics physics = ym.get(e); final Pos pos = pm.get(e); final Bounds bounds = bm.get(e); // no math required here. if (physics.vx != 0 || physics.vy != 0) { float px = pos.x + physics.vx * world.delta; float py = pos.y + physics.vy * world.delta; if ((physics.vx > 0 && collides(px + bounds.x2, py + bounds.y1 + (bounds.y2 - bounds.y1) * 0.5f)) || (physics.vx < 0 && collides(px + bounds.x1, py + bounds.y1 + (bounds.y2 - bounds.y1) * 0.5f))) { physics.vx = physics.bounce > 0 ? -physics.vx * physics.bounce : 0; px = pos.x; } if ((physics.vy > 0 && collides(px + bounds.x1 + (bounds.x2 - bounds.x1) * 0.5f, py + bounds.y2)) || (physics.vy < 0 && collides(px + bounds.x1 + (bounds.x2 - bounds.x1) * 0.5f, py + bounds.y1))) { physics.vy = physics.bounce > 0 ? -physics.vy * physics.bounce : 0; } } }
// keeps track of time, and calls methods that need to be called every drop public static void timeKeeping() { // random place stuff int rantime = 0; // the startTime helps keep track of how much time has passed since the loop started, which // keeps the computer's performance from effecting game speed long startTime = System.currentTimeMillis(); Main.display.repaint(); sleep(startTime); // a loop that calls methods after every drop while (Main.gameOn) { startTime = System.currentTimeMillis(); Physics.clearLineCheck(); Physics.dropCall(); Square.neighborFindCall(); Main.display.repaint(); sleep(startTime); // random place stuff rantime++; if (rantime > 3) { Square.randomPlace(); rantime = 0; } } }
/** * Causes this GameObject to jump if the jumping flag is set and this object is not already * jumping. */ public void setJumping(boolean isJumping) { if (isJumping() != isJumping) { super.setJumping(isJumping); if (isJumping) { Physics.getInstance().jump(this, jumpVelocity); } } }
public void tick() { if (Physics.Collision(this, game.ea)) { c.removeEntity(this); game.setEnemy_killed(game.getEnemy_killed() + 1); } y += speed; if (y > Game.HEIGHT * Game.SCALE) { speed = r.nextInt(3) + 1; x = r.nextInt(640); y = -10; } }
public void script() { time += Main.delay / 1000f; integerTime = (int) time; if (oldTime == integerTime) { nowNewSecond = false; // return; } else { nowNewSecond = true; oldTime = integerTime; } if (integerTime % 6 == 0 && nowNewSecond) { level++; } moveOnStep = gameConfiguration.moveOnStep; freakChanger = gameConfiguration.freakChanger; timeToPrism = gameConfiguration.timeToPrism; timeToObst = gameConfiguration.timeToObst; if (time - timeCfCreationBonus >= timeToPrism && (Main.game.gameConfiguration.playersAmount + Main.game.gameConfiguration.isBot <= 1)) { if (!obstacles.isEmpty()) { bonuses.add(new GOPrism()); while (Physics.checkCollisions( obstacles.get(obstacles.size() - 1), bonuses.get(bonuses.size() - 1))) { bonuses.remove(bonuses.size() - 1); bonuses.add(new GOPrism()); } } else bonuses.add(new GOPrism()); timeCfCreationBonus = time; } if (level > 7) { if (time - timeCfCreationObstacle >= timeToObst) { obstacles.add(new GOObstacle()); timeCfCreationObstacle = time; } } else { if (time - timeCfCreationObstacle >= timeToObst && time - timeCfCreationBonus > 0.2f) { obstacles.add(new GOObstacle()); timeCfCreationObstacle = time; } } }
@Override public void update() { if (x < 0) { x = 1; } // System.out.println(viX); if (GameB.mouseCont) { this.x = (Mouse.getX()) - this.sx / 2; } if (Keyboard.getEventKey() == Keyboard.KEY_D) { if (Keyboard.getEventKeyState()) { // if(x<0 ){x=0;x-=SPEED*mag;} // else if(x>Display.getWidth()-this.sx){x=Display.getWidth()-sx;x-=SPEED*mag;} // else if (SPEED < 5) { SPEED += 0.2f; } if (x + sx < Display.getWidth() && x > 0) { x += SPEED; viX = (float) Math.pow(SPEED, 2); } // System.out.println("A Key Pressed"); } else { if (SPEED > 0) { SPEED -= 0.2f; if (x + sx < Display.getWidth() && x >= 0) { x += SPEED; viX = (float) Math.pow(SPEED, 2); } else { x = Display.getWidth() - 1 - sx; viX = (float) Math.pow(SPEED, 2); } } // System.out.println("A Key Released"); } } if (Keyboard.getEventKey() == Keyboard.KEY_A) { if (Keyboard.getEventKeyState()) { // System.out.println("A Key Pressed"); if (SPEED < 5) { SPEED += 0.2f; viX = (float) Math.pow(SPEED, 2) * -1; } if (x < Display.getWidth() && x >= 3) { x += SPEED * -1; viX = (float) Math.pow(SPEED, 2) * -1; } } else { // System.out.println("A Key Released"); if (SPEED > 0) { SPEED -= 0.2f; if (x < Display.getWidth() && x > 0) { x -= SPEED; viX = (float) Math.pow(SPEED, 2) * -1; } else { x = 2; viX = 0; } // viX=(int)SPEED; } } } // System.out.println(a + " " + t); /* * * * if(ftime2 == 0){ftime2 = 1;ciX=this.x;t=0;viX = velF; a*=-1;} * t=t+(float)(0.08f); * * if(this.x<Display.getWidth()-this.sx){this.x = * .5f*(a)*t*t+viX*t+ciX;} * if(velF-1<0){ciX=this.x;ftime=0;a*=-1;System.out * .println("changed2");System.out.println(aO);} } else * if(Keyboard.isKeyDown(Keyboard.KEY_A)==false && * Keyboard.isKeyDown(Keyboard.KEY_D)==false && (ftime3==1) &&velF2<0 ){ * * if(ftime4 == 0){;ftime4 = 1;ciX=this.x;t2=0;viX = velF2; aO*=-1;} * t2=t2+(float)(0.08f); * * if(this.x>0){this.x = .5f*(aO)*t2*t2+viX*t2+ciX;} * if(velF2+1>0){ftime3 * =0;ciX=this.x;aO*=-1;viX=0;System.out.println("changed" * );System.out.println(aO);} * * } if(this.x<0){this.x=1;} */ // if(ball.getY()>Display.getHeight()){ball.resetPosition();Game.pScore++; // System.out.println(Game.pScore);} // if(ball.getY()<0){ball.resetPosition();Game.eScore++; // System.out.println(Game.eScore);} if (Physics.checkCollisions(this, ball) && this.y - sy <= ball.y) { // a=4/Math.abs(ball.x-x); float diff = Math.abs(ball.x-x); // ball.reverseY(getCenterX()); //getCenterY() FORCES TESTING Trig // calculations are not the best solution. I resorted to simplified // compoenent method. // ball.whywouldthiswork2(); // ball.viY = 0; // ball.viX = 0; // ball.a = 0; // ball.t = 0; // ball.ci = this.y-this.getSY(); /* * float resFx = ball.velF * * (float)Math.acos(ball.velF/ball.x)+this.viX; * * float resFy = ball.vfY*(float)Math.asin(ball.vfY/ball.x); float * test = (float)Math.sqrt(Math.pow(resFx, 2)+Math.pow(resFy, 2)); * float resF = test*(float)Math.atan(resFy/resFx); ball.t = 0; * ball.ci = this.y-this.getSY(); ball.ciX= this.x; ball.y--; * ball.viX = resFx; ball.viY = resFy; */ // ball.reverseY(); ball.reverseY(); float resFx = ball.viX + this.viX; // float resFy = ball.vfY; if ((int) ball.viX == 0) {} ball.viX = (ball.viX + (this.viX) * 2) * .45f; // ball.viY= -resFy; } // this.sy = SIZEY; }
/** Sets how high this GameObject can jump. */ public void setJumpHeight(float jumpHeight) { jumpVelocity = Physics.getInstance().getJumpVelocity(jumpHeight); }
/** @param phys Physik-Engine Objekt, dessen Inhalte dargestellt werden sollen */ public DrawPanel(final Physics phys) { super(); this.phys = phys; /* * Größenanpassung an Physik-Engine */ this.setSize(phys.getSize().getX().intValue(), phys.getSize().getY().intValue()); paused = false; actualColor = Color.black; frames = 1; history = true; mousePressed = false; directionArrow = false; secondMouseCoordinate = new Coordinate(); /* * Events, zum Erstellen neuer Bälle */ this.addMouseListener( new MouseListener() { @Override public void mouseClicked(MouseEvent me) { // System.out.println("mouse clicked"); // phys.addBall(new Ball(newBall.getRadius(), newBall.getPosition(), newBall.getSpeed(), // newBall.getFriction(), newBall.getBounce(), actualColor));//new // Color(actualColor.getRed(), actualColor.getGreen(), actualColor.getBlue()))); // System.out.println(me.getButton()); } @Override public void mousePressed(MouseEvent me) { mousePressed = true; } @Override public void mouseReleased(MouseEvent me) { mousePressed = false; Coordinate speed = new Coordinate(); speed.setX((newBall.getPosition().getX() - secondMouseCoordinate.getX()) / -50); speed.setY((newBall.getPosition().getY() - secondMouseCoordinate.getY()) / -50); phys.addBall( new Ball( newBall.getRadius(), newBall.getPosition(), speed, newBall.getFriction(), newBall.getBounce(), actualColor)); // new Color(actualColor.getRed(), actualColor.getGreen(), // actualColor.getBlue()))); } @Override public void mouseEntered(MouseEvent me) { // throw new UnsupportedOperationException("Not supported yet."); } @Override public void mouseExited(MouseEvent me) { // throw new UnsupportedOperationException("Not supported yet."); } }); /* * Event (Scrollen) zum Einstellen der Größe des neuen Balls */ this.addMouseWheelListener( new MouseWheelListener() { @Override public void mouseWheelMoved(MouseWheelEvent mwe) { if ((newBall.getRadius() - mwe.getWheelRotation()) >= 1) { newBall.setRadius(newBall.getRadius() - mwe.getWheelRotation()); } } }); newBall = new Ball(50.0, new Coordinate(100.0, 100.0), new Coordinate(1.0, 1.0), 0.0, 0.0, null); }
/* * Zeichenmethode zum Zeichnen der Bälle, etc. */ @Override public void paint(Graphics g) { /* * FPS-Zäler (Ausgabe findet erst am Ende von <code>paint(Graphics g)</code>) */ if (frames == 1) { time = System.currentTimeMillis(); } if (System.currentTimeMillis() - time != 0) { fps = (int) ((1000 * frames) / ((System.currentTimeMillis() - time))); } if (frames > 1000) { frames = 0; } frames++; /* * Bild für Doublebuffering erstellen */ dbImage = createImage(phys.getSize().getX().intValue() + 2, phys.getSize().getY().intValue() + 2); dbGraphics = dbImage.getGraphics(); /* * <code>phys.tick()</code> gehört nicht in die <code>paint(Graphics g)</code>-Methode * in Draw-Panel, es ist jedoch nicht sehr sinvoll diesen Abschnitt auszulagern, * da man sonst die Synchronisation der Threads (diese werden von Swing (GUI-Bibliothek) * automatisch erstellt) selbst schreiben müsste. Dies geschieht durch den Aufruf in * <code>paint(Graphics g)</code> automatisch. */ if (this.isPaused() == false) { phys.tick(); } /* * Bälle zeichnen */ Stack<Ball> baelle = phys.getBaelle(); int anzBaelle = baelle.size(); for (int i = 0; i < anzBaelle; i++) { dbGraphics.setColor(baelle.elementAt(i).getColor()); int x = baelle.elementAt(i).getPosition().getX().intValue() - baelle.elementAt(i).getRadius().intValue(); int y = baelle.elementAt(i).getPosition().getY().intValue() - baelle.elementAt(i).getRadius().intValue(); int d = baelle.elementAt(i).getRadius().intValue() * 2; dbGraphics.drawOval(x, y, d, d); /* * Pfad des Balls zeichnen, falls aktiviert */ if (history == true) { Stack<Coordinate> hist = baelle.elementAt(i).getHistory(); int oldX = baelle.elementAt(i).getPosition().getX().intValue(); int oldY = baelle.elementAt(i).getPosition().getY().intValue(); for (int h = hist.size() - 1; h != 0; h--) { Color c = new Color( baelle.elementAt(i).getColor().getRed(), baelle.elementAt(i).getColor().getGreen(), baelle.elementAt(i).getColor().getBlue(), (255 * h / hist.size())); dbGraphics.setColor(c); dbGraphics.drawLine( oldX, oldY, hist.get(h).getX().intValue(), hist.get(h).getY().intValue()); oldX = hist.get(h).getX().intValue(); oldY = hist.get(h).getY().intValue(); } } /* * Richtungspfeil zeichnen, falls aktiviert */ if (directionArrow == true) { dbGraphics.setColor(baelle.elementAt(i).getColor()); drawArrow( (Graphics2D) dbGraphics, baelle.elementAt(i).getPosition().getX().intValue(), baelle.elementAt(i).getPosition().getY().intValue(), baelle.elementAt(i).getPosition().getX() + (baelle.elementAt(i).getSpeed().getX() * 50), baelle.elementAt(i).getPosition().getY() + (baelle.elementAt(i).getSpeed().getY() * 50)); } } /* * Erfassen der Mausposition und Zeichnen der Vorlage für den nächsten * zu erzeugenden Ball */ int x = newBall.getPosition().getX().intValue() - newBall.getRadius().intValue(); int y = newBall.getPosition().getY().intValue() - newBall.getRadius().intValue(); int d = newBall.getRadius().intValue() * 2; if (mousePressed == false) { dbGraphics.setColor(Color.red); } else { dbGraphics.setColor(Color.green); } dbGraphics.drawOval(x, y, d, d); if (mousePressed == true) { dbGraphics.drawLine( newBall.getPosition().getX().intValue(), newBall.getPosition().getY().intValue(), secondMouseCoordinate.getX().intValue(), secondMouseCoordinate.getY().intValue()); } dbGraphics.setColor(Color.black); dbGraphics.drawRect(0, 0, phys.getSize().getX().intValue(), phys.getSize().getY().intValue()); Point mousePos = getMousePosition(); if (mousePos != null && mousePressed == false) { newBall.setPosition(new Coordinate(new Double(mousePos.x), new Double(mousePos.y))); } if (mousePos != null && mousePressed == true) { secondMouseCoordinate = new Coordinate(new Double(mousePos.x), new Double(mousePos.y)); } /* * Ausgeben der FPS-Zahl */ dbGraphics.drawString("FPS: " + String.valueOf(fps), 10, 20); g.drawImage(dbImage, 0, 0, this); }