Skip to content
Home
Post Code
Jass (Warcraft III)
Wurst
GUI (Warcraft III)
Galaxy (StarCraft II)
GUI (StarCraft II)
C++
Java
Delphi
Text
Search Code
Compare Code
Uploads
Draw
Create New Drawing
View Drawings
Graph
Log in
<pre class="jasscode">import java<span class="style_symbol">.</span>awt<span class="style_symbol">.</span>Rectangle; import java<span class="style_symbol">.</span>util<span class="style_symbol">.</span>ArrayList; <span class="style_keyword">public</span> <span class="style_keyword">class</span> Collision <span class="style_comment">{</span> <span class="style_comment"></span> <span class="style_comment"> Driver driver;</span> <span class="style_comment"></span> <span class="style_comment"> // Arrayliste für die Spriteobjekte anlegen</span> <span class="style_comment"> ArrayList<WorldObject> spriteListWorld;</span> <span class="style_comment"></span> <span class="style_comment"> // kleine Korrektion für genauere grafische Darstellung</span> <span class="style_comment"> int collisionCorrection = 6;</span> <span class="style_comment"></span> <span class="style_comment"> // Checksummen für Anzahl der Kollisionen</span> <span class="style_comment"> int checkSumRight = 0;</span> <span class="style_comment"> int checkSumLeft = 0;</span> <span class="style_comment"></span> <span class="style_comment"></span> <span class="style_comment"> // Konstruktor für die Sprites</span> <span class="style_comment"> public Collision(Driver driver, ArrayList<WorldObject> spriteListWorld) {</span> <span class="style_comment"> this.driver = driver;</span> <span class="style_comment"> this.spriteListWorld = spriteListWorld;</span> <span class="style_comment"></span> <span class="style_comment"> // Erstellen der Objekt Rechtecke</span> <span class="style_comment"> for (WorldObject sprite : spriteListWorld) {</span> <span class="style_comment"> sprite.rectangle = new Rectangle((int)sprite.x, (int)sprite.y, sprite.width, sprite.height);</span> <span class="style_comment"> }</span> } <span class="style_comment">// Kollisionsabfrage für Objekte</span> <span class="style_keyword">public</span> void collisionObjects() <span class="style_comment">{</span> <span class="style_comment"></span> <span class="style_comment"> // Kollisionsabfragen des Driverobjekts mit den Worldobjekten</span> <span class="style_comment"> for (WorldObject sprite : spriteListWorld) {</span> <span class="style_comment"></span> <span class="style_comment">// // wechselt Kollisionsabfragen bei Imagewechsel</span> <span class="style_comment">// driver.DriverFlipImage();</span> <span class="style_comment"></span> <span class="style_comment"> // Erstellen des Driver Rechtecks</span> <span class="style_comment"> driver.rectangle = new Rectangle((int)driver.x, (int)driver.y, driver.width, driver.height);</span> <span class="style_comment"></span> <span class="style_comment"></span> <span class="style_comment"> // Abfrage Driver kollidiert mit oberer Kante eines Objekts und fährt auf dem Objekt weiter</span> <span class="style_comment"> if (driver.rectangle.intersectsLine(sprite.rectangle.getMinX() + collisionCorrection, sprite.rectangle.getMinY(),</span> <span class="style_comment"> sprite.rectangle.getMaxX() - collisionCorrection, sprite.rectangle.getMinY())) {</span> <span class="style_comment"> driver.y = sprite.rectangle.getMinY() - driver.height - collisionCorrection;</span> <span class="style_comment"> driver.landing = true;</span> <span class="style_comment"> }</span> <span class="style_comment">// Abfrage Driver kollidiert mit unterer Kante eines Objekts</span> <span class="style_keyword">if</span> (driver<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>intersectsLine(sprite<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMinX() <span class="style_symbol">+</span> collisionCorrection<span class="style_symbol">,</span> sprite<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMaxY()<span class="style_symbol">,</span> sprite<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMaxX() <span class="style_symbol">-</span> collisionCorrection<span class="style_symbol">,</span> sprite<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMaxY())) <span class="style_comment">{</span> <span class="style_comment"> driver.jumping = false;</span> <span class="style_comment"> }</span> <span class="style_comment">// Kollisionsabfrage rechte Kante des Objekts (in der Folge kann der Driver solange nicht weiter nach Links)</span> <span class="style_keyword">if</span> (sprite<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>intersectsLine(driver<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMinX()<span class="style_symbol">,</span> driver<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMinY() <span class="style_symbol">+</span> collisionCorrection<span class="style_symbol">,</span> driver<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMinX()<span class="style_symbol">,</span> driver<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMaxY() <span class="style_symbol">-</span> collisionCorrection)) <span class="style_comment">{</span> <span class="style_comment"> driver.collideLeft = true;</span> <span class="style_comment"> sprite.collideRight = true;</span> <span class="style_comment"> }</span> <span class="style_comment">// Kollisionsabfrage linke Kante des Objekts (in der Folge kann der Driver solange nicht weiter nach Rechts)</span> <span class="style_keyword">if</span> (sprite<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>intersectsLine(driver<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMaxX()<span class="style_symbol">,</span> driver<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMinY() <span class="style_symbol">+</span> collisionCorrection<span class="style_symbol">,</span> driver<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMaxX()<span class="style_symbol">,</span> driver<span class="style_symbol">.</span>rectangle<span class="style_symbol">.</span>getMaxY() <span class="style_symbol">-</span> collisionCorrection)) <span class="style_comment">{</span> <span class="style_comment"> driver.collideRight = true;</span> <span class="style_comment"> sprite.collideLeft = true;</span> <span class="style_comment"> }</span> checkSum(); } } <span class="style_comment">// Kollisionsabfrage für Gegner</span> <span class="style_keyword">public</span> void collisionEnemy() <span class="style_comment">{</span> <span class="style_comment"></span> <span class="style_comment"> }</span> <span class="style_comment">// benötigte Checksumme der Kolisionen durch generierte Objekte</span> <span class="style_keyword">private</span> void checkSum() <span class="style_comment">{</span> <span class="style_comment"></span> <span class="style_comment"> // Gegenrechnen der Checksummen für Rechts und Links (erst wenn die Checksumme 0 ergibt kann der Driver in diese Richtung weiterfahren)</span> <span class="style_comment"> for (WorldObject sprite : spriteListWorld) {</span> <span class="style_comment"></span> <span class="style_comment"> // Checksumme Rechts</span> <span class="style_comment"> if (sprite.collideRight == true) {</span> <span class="style_comment"> checkSumRight++;</span> <span class="style_comment"> }</span> <span class="style_comment">// Checksumme Links</span> <span class="style_keyword">if</span> (sprite<span class="style_symbol">.</span>collideLeft <span class="style_symbol">=</span><span class="style_symbol">=</span> <span class="style_keyword">true</span>) <span class="style_comment">{</span> <span class="style_comment"> checkSumLeft++;</span> <span class="style_comment"> }</span> <span class="style_comment">// Reset der boolschen Checkvariablen</span> sprite<span class="style_symbol">.</span>collideRight <span class="style_symbol">=</span> <span class="style_keyword">false</span>; sprite<span class="style_symbol">.</span>collideLeft <span class="style_symbol">=</span> <span class="style_keyword">false</span>; } <span class="style_comment">// Wenn Checksumme gleich 0 darf der Driver weiterfahren (links)</span> <span class="style_keyword">if</span> (checkSumRight <span class="style_symbol">=</span><span class="style_symbol">=</span> <span class="style_int">0</span>) <span class="style_comment">{</span> <span class="style_comment"> driver.collideLeft = false;</span> <span class="style_comment"> }</span> <span class="style_keyword">else</span> <span class="style_comment">{</span> <span class="style_comment"> checkSumRight = 0;</span> <span class="style_comment"> }</span> <span class="style_comment">// Wenn Checksumme gleich 0 darf der Driver weiterfahren (rechts)</span> <span class="style_keyword">if</span> (checkSumLeft <span class="style_symbol">=</span><span class="style_symbol">=</span> <span class="style_int">0</span>) <span class="style_comment">{</span> <span class="style_comment"> driver.collideRight = false;</span> <span class="style_comment"> }</span> <span class="style_keyword">else</span> <span class="style_comment">{</span> <span class="style_comment"> checkSumLeft = 0;</span> <span class="style_comment"> }</span> } } </pre>
select language
jass
java
> delphi
C++
php
GSL
galaxy
visualBasic
wurst
Text
Markdown
select style
default
bbcode - default
Jasscraft
> jasscraft html
visual
lightweight
lightweightNr
goto line:
Compare with:
text copy window
edit this code
post new code
import java.awt.Rectangle; import java.util.ArrayList; public class Collision { Driver driver; // Arrayliste für die Spriteobjekte anlegen ArrayList<WorldObject> spriteListWorld; // kleine Korrektion für genauere grafische Darstellung int collisionCorrection = 6; // Checksummen für Anzahl der Kollisionen int checkSumRight = 0; int checkSumLeft = 0; // Konstruktor für die Sprites public Collision(Driver driver, ArrayList<WorldObject> spriteListWorld) { this.driver = driver; this.spriteListWorld = spriteListWorld; // Erstellen der Objekt Rechtecke for (WorldObject sprite : spriteListWorld) { sprite.rectangle = new Rectangle((int)sprite.x, (int)sprite.y, sprite.width, sprite.height); } } // Kollisionsabfrage für Objekte public void collisionObjects() { // Kollisionsabfragen des Driverobjekts mit den Worldobjekten for (WorldObject sprite : spriteListWorld) { // // wechselt Kollisionsabfragen bei Imagewechsel // driver.DriverFlipImage(); // Erstellen des Driver Rechtecks driver.rectangle = new Rectangle((int)driver.x, (int)driver.y, driver.width, driver.height); // Abfrage Driver kollidiert mit oberer Kante eines Objekts und fährt auf dem Objekt weiter if (driver.rectangle.intersectsLine(sprite.rectangle.getMinX() + collisionCorrection, sprite.rectangle.getMinY(), sprite.rectangle.getMaxX() - collisionCorrection, sprite.rectangle.getMinY())) { driver.y = sprite.rectangle.getMinY() - driver.height - collisionCorrection; driver.landing = true; } // Abfrage Driver kollidiert mit unterer Kante eines Objekts if (driver.rectangle.intersectsLine(sprite.rectangle.getMinX() + collisionCorrection, sprite.rectangle.getMaxY(), sprite.rectangle.getMaxX() - collisionCorrection, sprite.rectangle.getMaxY())) { driver.jumping = false; } // Kollisionsabfrage rechte Kante des Objekts (in der Folge kann der Driver solange nicht weiter nach Links) if (sprite.rectangle.intersectsLine(driver.rectangle.getMinX(), driver.rectangle.getMinY() + collisionCorrection, driver.rectangle.getMinX(), driver.rectangle.getMaxY() - collisionCorrection)) { driver.collideLeft = true; sprite.collideRight = true; } // Kollisionsabfrage linke Kante des Objekts (in der Folge kann der Driver solange nicht weiter nach Rechts) if (sprite.rectangle.intersectsLine(driver.rectangle.getMaxX(), driver.rectangle.getMinY() + collisionCorrection, driver.rectangle.getMaxX(), driver.rectangle.getMaxY() - collisionCorrection)) { driver.collideRight = true; sprite.collideLeft = true; } checkSum(); } } // Kollisionsabfrage für Gegner public void collisionEnemy() { } // benötigte Checksumme der Kolisionen durch generierte Objekte private void checkSum() { // Gegenrechnen der Checksummen für Rechts und Links (erst wenn die Checksumme 0 ergibt kann der Driver in diese Richtung weiterfahren) for (WorldObject sprite : spriteListWorld) { // Checksumme Rechts if (sprite.collideRight == true) { checkSumRight++; } // Checksumme Links if (sprite.collideLeft == true) { checkSumLeft++; } // Reset der boolschen Checkvariablen sprite.collideRight = false; sprite.collideLeft = false; } // Wenn Checksumme gleich 0 darf der Driver weiterfahren (links) if (checkSumRight == 0) { driver.collideLeft = false; } else { checkSumRight = 0; } // Wenn Checksumme gleich 0 darf der Driver weiterfahren (rechts) if (checkSumLeft == 0) { driver.collideRight = false; } else { checkSumLeft = 0; } } }