Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / GameObject.java
  1. package gameEngine.entites;
  2.  
  3. import gameEngine.entites.gameComponents.GameComponent;
  4. import gameEngine.entites.gameComponents.Mesh;
  5. import gameEngine.entites.gameComponents.TextMesh;
  6. import gameEngine.scenes.GameScene;
  7. import gameEngine.views.Window;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. public class GameObject extends Entity {
  13. public final List<GameComponent> gameComponents = new ArrayList<>();
  14.  
  15. public GameObject(String id) {
  16. super(id);
  17. }
  18.  
  19. public void addComponent(GameComponent component) {
  20. for (GameComponent existingComponent : gameComponents) {
  21. if (existingComponent.getClass().equals(component.getClass())) {
  22. System.out.println("Component already exists: " + component.getClass().getSimpleName());
  23. return;
  24. }
  25. }
  26. this.gameComponents.add(component);
  27. System.out.println("Component added: " + component.getClass().getSimpleName());
  28. }
  29.  
  30. public void removeComponent(GameComponent component) {
  31. boolean removed = this.gameComponents.remove(component);
  32. if (removed) {
  33. System.out.println("Component removed: " + component.getClass().getSimpleName());
  34. } else {
  35. System.out.println("Component not found: " + component.getClass().getSimpleName());
  36. }
  37. }
  38.  
  39. public void initComponents() {
  40. for (GameComponent component : gameComponents) {
  41. component.init();
  42. }
  43. }
  44.  
  45. public void updateComponents() {
  46. for (GameComponent component : gameComponents) {
  47. component.update();
  48. }
  49. }
  50.  
  51. public <T extends GameComponent> T getComponent(Class<T> componentClass){
  52. for (GameComponent gameComponent :gameComponents) {
  53. if (componentClass.isInstance(gameComponent)) {
  54. return componentClass.cast(gameComponent);
  55. }
  56. }
  57. return null;
  58. }
  59.  
  60. }