Advertisement
Guest User

Untitled

a guest
May 4th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. package dk.sdu.mmmi.cbse.project6.asteroids;
  2.  
  3. import com.decouplink.DisposableList;
  4. import static com.decouplink.Utilities.context;
  5. import dk.sdu.mmmi.cbse.project6.common.data.BehaviourEnum;
  6. import dk.sdu.mmmi.cbse.project6.common.data.Entity;
  7. import dk.sdu.mmmi.cbse.project6.common.data.EntityType;
  8. import static dk.sdu.mmmi.cbse.project6.common.data.EntityType.PLAYER;
  9. import dk.sdu.mmmi.cbse.project6.common.data.GameTime;
  10. import dk.sdu.mmmi.cbse.project6.common.data.Position;
  11. import dk.sdu.mmmi.cbse.project6.common.data.Rotation;
  12. import dk.sdu.mmmi.cbse.project6.common.data.Scale;
  13. import dk.sdu.mmmi.cbse.project6.common.data.View;
  14. import dk.sdu.mmmi.cbse.project6.common.services.IEntityProcessingService;
  15. import dk.sdu.mmmi.cbse.project6.common.services.IGamePluginService;
  16. import java.util.ArrayList;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import org.apache.tuscany.sca.Node;
  20. import org.oasisopen.sca.NoSuchServiceException;
  21. import playn.core.Game;
  22. import playn.core.GroupLayer;
  23. import playn.core.Image;
  24. import playn.core.ImageLayer;
  25. import playn.core.Keyboard;
  26. import playn.core.PlayN;
  27. import static playn.core.PlayN.assets;
  28. import static playn.core.PlayN.graphics;
  29. import playn.core.util.Clock;
  30.  
  31. public class AsteroidsGame extends Game.Default {
  32.  
  33. private final Clock.Source clock = new Clock.Source(33);
  34. private GroupLayer layer;
  35. private final Object world = new Object();
  36. private Node node;
  37. private Entity player;
  38. private ArrayList<IEntityProcessingService> EntityProcessingServices = new ArrayList<>();
  39.  
  40. public AsteroidsGame(Node node) {
  41. super(33); // call update every 33ms (30 times per second)
  42. this.node = node;
  43. }
  44.  
  45. @Override
  46. public void init() {
  47.  
  48. // Add clock to world context
  49. context(world).add(GameTime.class, new GameTime());
  50.  
  51. // Lookup all Game Plugins using ServiceLoader
  52. getPluginService().start(world);
  53.  
  54. PlayN.keyboard().setListener(keyboardListener);
  55.  
  56. // create a group layer to hold everything
  57. layer = graphics().rootLayer();
  58.  
  59. // create and add background image layer
  60. layer.add(graphics().createImmediateLayer(
  61. new StarRenderer(clock, world)));
  62.  
  63. // Create views for each entity
  64. createViews();
  65. for (Entity entity : context(world).all(Entity.class)) {
  66. if (context(entity).one(EntityType.class) == PLAYER) {
  67. this.player = entity;
  68. System.out.println("Link created");
  69. }
  70. }
  71.  
  72. EntityProcessingServices = getEntityProcessingService();
  73.  
  74. onInit();
  75. }
  76.  
  77. @Override
  78. public void update(int delta) {
  79.  
  80. clock.update(delta);
  81. context(world).one(GameTime.class).delta = delta;
  82.  
  83. // Process each entity using provided processing services (i.e.,
  84. // ServiceLoader services)
  85.  
  86. for (IEntityProcessingService p: EntityProcessingServices)
  87. {
  88. // IEntityProcessingService entityProcessorService = getEntityProcessingService();
  89. // for (Entity e : context(world).all(Entity.class)) {
  90. // entityProcessorService.process(world, e);
  91. // }
  92. // IEntityProcessingService entityProcessorService2 = getEntityProcessingService2();
  93. for (Entity e : context(world).all(Entity.class)) {
  94. p.process(world, e);
  95. }
  96. }
  97.  
  98.  
  99. }
  100.  
  101. @Override
  102. public void paint(float alpha) {
  103. // the background automatically paints itself, so no need to do anything
  104. // here!
  105. clock.paint(alpha);
  106.  
  107. for (Entity e : context(world).all(Entity.class)) {
  108. ImageLayer view = context(e).one(ImageLayer.class);
  109. Position p = context(e).one(Position.class);
  110. Rotation r = context(e).one(Rotation.class);
  111. Scale s = context(e).one(Scale.class);
  112.  
  113. view.setTranslation(p.x, p.y);
  114. view.setRotation(r.angle);
  115. view.setAlpha(1.0f);
  116. view.setScale(s.x, s.y);
  117. }
  118. }
  119.  
  120. private void createViews() {
  121. for (Entity entity : context(world).all(Entity.class)) {
  122.  
  123. View sprite = context(entity).one(View.class);
  124.  
  125. String imagePath = sprite.getImageFilePath();
  126.  
  127. Image image = assets().getImageSync(imagePath);
  128.  
  129. ImageLayer viewLayer = graphics().createImageLayer(image);
  130. viewLayer.setOrigin(image.width() / 2f, image.height() / 2f);
  131.  
  132. context(entity).add(ImageLayer.class, viewLayer);
  133. layer.add(viewLayer);
  134. }
  135. }
  136.  
  137. private IGamePluginService getPluginService() {
  138.  
  139. IGamePluginService gp = null;
  140.  
  141. try {
  142. gp = node.getService(IGamePluginService.class, "EntityComponent");
  143. } catch (NoSuchServiceException e) {
  144. // TODO Auto-generated catch block
  145. e.printStackTrace();
  146. }
  147.  
  148. return gp;
  149. }
  150.  
  151. private final Keyboard.Listener keyboardListener = new Keyboard.Listener() {
  152.  
  153. private final DisposableList disposables = new DisposableList();
  154.  
  155.  
  156. @Override
  157. public void onKeyDown(Keyboard.Event event) {
  158. switch (event.key()) {
  159. case W:
  160. disposables.add(context(player).add(BehaviourEnum.class, BehaviourEnum.MOVE_UP));
  161. break;
  162.  
  163. case S:
  164. disposables.add(context(player).add(BehaviourEnum.class, BehaviourEnum.MOVE_DOWN));
  165. break;
  166.  
  167. case A:
  168. disposables.add(context(player).add(BehaviourEnum.class, BehaviourEnum.MOVE_LEFT));
  169. break;
  170.  
  171. case D:
  172. disposables.add(context(player).add(BehaviourEnum.class, BehaviourEnum.MOVE_RIGHT));
  173. break;
  174.  
  175. // case SPACE:
  176. // context(player).add(BehaviourEnum.class, BehaviourEnum.SHOOT);
  177. // break;
  178.  
  179. default:
  180. break;
  181. }
  182. }
  183.  
  184. @Override
  185. public void onKeyTyped(Keyboard.TypedEvent te) {
  186. }
  187.  
  188. @Override
  189. public void onKeyUp(Keyboard.Event event) {
  190. disposables.dispose();
  191. }
  192. };
  193.  
  194. private ArrayList<IEntityProcessingService> getEntityProcessingService() {
  195. ArrayList<IEntityProcessingService> IEntityProcessingService = new ArrayList<>();
  196.  
  197.  
  198. try {
  199. System.out.println("got added 1");
  200. IEntityProcessingService.add(node.getService(IEntityProcessingService.class, "EntityProcessingComponent"));
  201. } catch (NoSuchServiceException e) {
  202. // TODO Auto-generated catch block
  203. e.printStackTrace();
  204. }
  205. try {
  206. System.out.println("got added 2");
  207. IEntityProcessingService.add(node.getService(IEntityProcessingService.class,
  208. "PlayerSystem"));
  209. } catch (NoSuchServiceException e) {
  210. // TODO Auto-generated catch block
  211. e.printStackTrace();
  212. }
  213. try {
  214. System.out.println("got added 3");
  215. IEntityProcessingService.add(node.getService(IEntityProcessingService.class,
  216. "MoveSystem"));
  217. } catch (NoSuchServiceException e) {
  218. // TODO Auto-generated catch block
  219. e.printStackTrace();
  220. }
  221.  
  222. return IEntityProcessingService;
  223. }
  224.  
  225. private void onInit() {
  226. for (Entity entity : context(world).all(Entity.class)) {
  227. if (context(entity).one(EntityType.class) == PLAYER) {
  228. this.player = entity;
  229. }
  230. }
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement