Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. // -------- Demo object oriented design ----------
  2.  
  3. // Try to rely as litle as possible on the global space, i will turn messy quick
  4. Scene ActiveScene;
  5.  
  6.  
  7. // used for setting up the staring logic of the game
  8. void setup(){
  9.     ActiveScene = new SpaceScene();
  10. }
  11.  
  12. // the game loop
  13. void draw(){
  14.     ActiveScene.Update();
  15.     ActiveScene.Draw();
  16. }
  17.  
  18. // base class for all scenes
  19. public abstract class Scene{
  20.    
  21.     // when a scene gets constructed, it will request the assets to be loaded
  22.     public Scene(){
  23.         LoadAssets();
  24.     }
  25.  
  26.     // the shell of a function responsible for loading assets into the scene.
  27.     protected abstract void LoadAssets();
  28.     // the shell of a function responsible for updating all objects in the scene
  29.     public abstract void Update();
  30.     // the shell of a function responsible for drawing all objects in the scene
  31.     public abstract void Draw();
  32. }
  33.  
  34. public class SpaceScene{
  35.     private PImage _asteroidImage;
  36.     private Asteroid _asteroid;
  37.  
  38.     // construct the space scene
  39.     public SpaceScene(){
  40.         super();
  41.  
  42.         _asteroid = new Asteroid(_asteroidImage,200,200,10,10);
  43.     }
  44.  
  45.     // load al the assets in the space scene
  46.     protected void LoadAssets(){
  47.         _asteroidImage = loadImage("asteroid.png");
  48.     }
  49.  
  50.     // update all logic related to the space scene
  51.     public void Update(){
  52.         _asteroid.Update();
  53.     }
  54.  
  55.     // draw all objects in the space scene
  56.     public void Draw(){
  57.         // draw the background of the scene to prevent ghosting
  58.         background(0);
  59.  
  60.         _asteroid.Draw();
  61.     }
  62. }
  63.  
  64.  
  65. // base class for all game objects
  66. public abstract class GameObject{
  67.  
  68.     // the current location of the game object
  69.     public PVector Position;
  70.     public PVector Size;
  71.  
  72.     // the sprite of the gameobject
  73.     protected PImage _sprite;
  74.  
  75.     // constructors for the base class
  76.     public GameObject(PImage sprite, PVector position, PVector size){
  77.         Position = position
  78.         Size = size;
  79.         _sprite = sprite;
  80.     }
  81.  
  82.     // overloaded constructor to give more options of configuring the object
  83.     public GameObject(PImage sprite, float x, float y, float width, float height){
  84.         Position = new PVector(x,y);
  85.         Size = new PVector(width,height);
  86.         _sprite = sprite;
  87.     }
  88.  
  89.     // declare functions for the subclasses
  90.  
  91.     // execute the game logic of the game object
  92.     public abstract void Update();
  93.    
  94.     // draw the current game object
  95.     public virtual void Draw(){
  96.         image(_sprite,Position.x, Position.y,Size.x,Size.y)
  97.     };
  98. }
  99.  
  100. // sub class that inherits from GameObject
  101. public class Asteroid extends GameObject{
  102.    
  103.     // constructors for the base class
  104.     public GameObject(PImage sprite, PVector position, PVector size){
  105.         super(sprite, position, size);
  106.     }
  107.  
  108.     // overloaded constructor to give more options of configuring the object
  109.     public GameObject(PImage sprite, float x, float y, float width, float height){
  110.         super(sprite, x, y, width, height);
  111.     }
  112.  
  113.     // object specific logic update
  114.     public void Update{
  115.         Position.y += 10;
  116.     }
  117.  
  118.     // draw is missing, as the base class has already defined basic behavior
  119.     // you can make your own draw logic by overriding the method
  120. }
  121.  
  122. /*
  123. * References:
  124. * PVector - https://processing.org/reference/PVector.html
  125. * java keywords - https://www.w3schools.com/java/java_ref_keywords.asp
  126. * inheritance - https://www.w3schools.com/java/ref_keyword_extends.asp
  127. * polymorphism - https://www.w3schools.com/java/java_polymorphism.asp
  128. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement