1. import sprites.*; //librairie controlant les sprites
  2. import sprites.maths.*;
  3. import sprites.utils.*;
  4. Sprite explosion;
  5. StopWatch sw = new StopWatch();
  6. ArrayList<Entity> entities = new ArrayList<Entity>();
  7. ArrayList<Entity> toRemove = new ArrayList<Entity>();
  8. double dTime;
  9. long prevTime;
  10. void setup()
  11. {
  12. size(500, 500, P3D);
  13. frameRate(120);
  14. background(0);
  15. entities.add(new Joueur(100, 300));
  16. }
  17. void draw()
  18. {
  19. dTime = (prevTime - (prevTime = frameRateLastNanos))/-1e7d;
  20. for (int i = entities.size()-1; i >= 0; i--)
  21. {
  22. Entity entity = entities.get(i);
  23. entity.display();
  24. entity.update();
  25. if (entity.x > 300 ) toRemove.add(entities.get(i));
  26. }
  27. for (Entity e : entities)
  28. {
  29. if (e.destroyed) toRemove.add(e);
  30. }
  31. for (Entity r : toRemove)
  32. {
  33. r.destroy();
  34. entities.remove(r);
  35. }
  36. }
  37. abstract class Entity {
  38. float x, y, w, h;
  39. boolean ally, destroyed = false;
  40. Entity(float x, float y, boolean ally)
  41. {
  42. this.x = x;
  43. this.y = y;
  44. this.ally = ally;
  45. }
  46. void update()
  47. {
  48. }
  49. void display()
  50. {
  51. rect(x, y, 20, 20);
  52. }
  53. void touched()
  54. {
  55. }
  56. void destroy()
  57. {
  58. }
  59. }
  60. class Vaisseau extends Entity {
  61. int vie;
  62. double delay;
  63. double delayTir;
  64. Vaisseau(float x, float y, boolean player)
  65. {
  66. super(x, y, player);
  67. this.ally = player;
  68. delayTir = ally ? 10 : random(200, 250);
  69. delay = (ally ? delayTir : delayTir/2) * dTime ;
  70. }
  71. void update()
  72. {
  73. S4P.updateSprites(dTime);
  74. S4P.drawSprites();
  75. }
  76. void tir()
  77. {
  78. //...
  79. }
  80. void touched()
  81. {
  82. vie -= 1;
  83. if (vie <= 0) destroyed = true;
  84. }
  85. void destroy()
  86. {
  87. explosion.setXY(x, y);
  88. explosion.setFrameSequence(0, 24, 0.02, 1);
  89. }
  90. }
  91. class Joueur extends Vaisseau {
  92. int vitesse;
  93. Joueur(float x, float y)
  94. {
  95. super(x, y, true);
  96. vitesse = 5;
  97. vie = 1;
  98. }
  99. void update()
  100. {
  101. x ++;
  102. }
  103. }