1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
public void moveFromMouse( int mouseX, int mouseY ) {
double oldX = getX();
double oldY = getY();
double deltaX;
double deltaY;
setPosition( mouseX, mouseY );
boolean magnet = false;
if( motor == null ) {
ArrayList<Piece> pieceMagnet = new ArrayList<Piece>(1);
for( int i = 0; i < Engrenage.getAllPiece().size(); i++ ) {
if( Engrenage.getAllPiece().get(i).isMagnetic(this) == 1 )
{
System.out.println("isMagnetic est = "+ Engrenage.getAllPiece().get(i).isMagnetic(this));
pieceMagnet.add( Engrenage.getAllPiece().get(i) );
}
}
if( pieceMagnet.size() > 0 ) {
pieceMagnet.get(0).magnetization(this);
deltaX = getX() - oldX;
deltaY = getY() - oldY;
if( linkedAxis.linkedGear.size() == 1 ) {
linkedAxis.moveFromGear( deltaX, deltaY, this );
}
else // This se détache de son axe !
linkedAxis.removeLinkedGear(this);
for(int j = 0; j < linkedGear.size(); j++) {
linkedGear.get(j).moveFromGear( deltaX, deltaY );
}
magnet = true;
}
}
if( !magnet ) {
System.out.println("!magnet de "+ this);
deltaX = getX() - oldX;
deltaY = getY() - oldY;
if ( linkedGearUp != null ) {
linkedGearUp.removeLinkedGear(this);
linkedGearUp = null;
}
if( linkedAxis.linkedGear.size() == 1 ) {
linkedAxis.moveFromGear( deltaX, deltaY, this );
}
else
linkedAxis.removeLinkedGear(this);
for(int i = 0; i < linkedGear.size(); i++) {
linkedGear.get(i).moveFromGear( deltaX, deltaY );
}
}
}
public int isMagnetic( Piece pieceFrom ) {
System.out.println("isMagnetic de "+ this);
if( magnetic ) {
if( pieceFrom instanceof Gear ) {
Gear gear = (Gear) pieceFrom;
boolean ok = true;
Gear tempGearUp = this;
if( tempGearUp == gear ) // VRAIMENT UTILE ??
ok = false;
while( tempGearUp.linkedGearUp != null ) {
tempGearUp = tempGearUp.linkedGearUp;
if( tempGearUp == gear )
ok = false;
}
double delta = Math.sqrt(Math.pow(getX()-gear.getX(), 2) + Math.pow(getY()-gear.getY(), 2)) - pitchC-gear.pitchC;
System.out.println("Delta entre "+ this+" et "+gear+" = "+delta);
if( ok && delta < 40 && delta > -40 ) {
System.out.println("TRUE");
return 1;
}
}
}
System.out.println("FALSE");
return 0;
}
public void magnetization( Piece pieceFrom ) {
System.out.println("magnetization de "+ this+" et de "+ pieceFrom);
if( pieceFrom instanceof Gear ) {
Gear gear = (Gear) pieceFrom;
if( gear.linkedGearUp != null )
gear.linkedGearUp.removeLinkedGear(gear);
gear.linkedGearUp = this;
this.setLinkedGear(gear);
double dist = Math.sqrt(Math.pow(getX()-gear.getX(), 2) + Math.pow(getY()-gear.getY(), 2));
int x = (int) (getX()+( (gear.getX()-getX()) * (pitchC + gear.pitchC) / dist) );
int y = (int) (getY()+( (gear.getY()-getY()) * (pitchC + gear.pitchC) / dist) );
gear.setPosition(x, y);
gear.meshing();
}
} |
Partager