Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -------- Demo object oriented design ----------
- // Try to rely as litle as possible on the global space, i will turn messy quick
- Scene ActiveScene;
- // used for setting up the staring logic of the game
- void setup(){
- ActiveScene = new SpaceScene();
- }
- // the game loop
- void draw(){
- ActiveScene.Update();
- ActiveScene.Draw();
- }
- // base class for all scenes
- public abstract class Scene{
- // when a scene gets constructed, it will request the assets to be loaded
- public Scene(){
- LoadAssets();
- }
- // the shell of a function responsible for loading assets into the scene.
- protected abstract void LoadAssets();
- // the shell of a function responsible for updating all objects in the scene
- public abstract void Update();
- // the shell of a function responsible for drawing all objects in the scene
- public abstract void Draw();
- }
- public class SpaceScene{
- private PImage _asteroidImage;
- private Asteroid _asteroid;
- // construct the space scene
- public SpaceScene(){
- super();
- _asteroid = new Asteroid(_asteroidImage,200,200,10,10);
- }
- // load al the assets in the space scene
- protected void LoadAssets(){
- _asteroidImage = loadImage("asteroid.png");
- }
- // update all logic related to the space scene
- public void Update(){
- _asteroid.Update();
- }
- // draw all objects in the space scene
- public void Draw(){
- // draw the background of the scene to prevent ghosting
- background(0);
- _asteroid.Draw();
- }
- }
- // base class for all game objects
- public abstract class GameObject{
- // the current location of the game object
- public PVector Position;
- public PVector Size;
- // the sprite of the gameobject
- protected PImage _sprite;
- // constructors for the base class
- public GameObject(PImage sprite, PVector position, PVector size){
- Position = position
- Size = size;
- _sprite = sprite;
- }
- // overloaded constructor to give more options of configuring the object
- public GameObject(PImage sprite, float x, float y, float width, float height){
- Position = new PVector(x,y);
- Size = new PVector(width,height);
- _sprite = sprite;
- }
- // declare functions for the subclasses
- // execute the game logic of the game object
- public abstract void Update();
- // draw the current game object
- public virtual void Draw(){
- image(_sprite,Position.x, Position.y,Size.x,Size.y)
- };
- }
- // sub class that inherits from GameObject
- public class Asteroid extends GameObject{
- // constructors for the base class
- public GameObject(PImage sprite, PVector position, PVector size){
- super(sprite, position, size);
- }
- // overloaded constructor to give more options of configuring the object
- public GameObject(PImage sprite, float x, float y, float width, float height){
- super(sprite, x, y, width, height);
- }
- // object specific logic update
- public void Update{
- Position.y += 10;
- }
- // draw is missing, as the base class has already defined basic behavior
- // you can make your own draw logic by overriding the method
- }
- /*
- * References:
- * PVector - https://processing.org/reference/PVector.html
- * java keywords - https://www.w3schools.com/java/java_ref_keywords.asp
- * inheritance - https://www.w3schools.com/java/ref_keyword_extends.asp
- * polymorphism - https://www.w3schools.com/java/java_polymorphism.asp
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement