How to create game objects correctly

I make sims like a game, and now I'm trying to figure out how I will structure my objects.

Now I'm going to create a class called GameObject, psuedo below

public class GameObject {
       name:String
       width:int
       height:int
}

In this way, I could create objects such as bushes, trees and buildings. But then I started to think. What if I wanted to create several buildings and trees of the same type? I would have to make GameObject instances and give it a new name, height and width. The properties must be the same so that I can duplicate one object. It seems a little tedious. Then I think maybe this is not the right way. So I thought: I will have to extend GameObject, as shown below

public class Tree extends GameObject{
       birdHouse:Boolean
}

public class Building extends GameObject{
       packingGarage:Boolean
       stories:Number
}

public class House extends GameObject{
      garage:Boolean
      stories:Number
}

, , . , , , .

, . - . , - . . , , ,

public class GameObject implement IGameObject {
           name:String
           width:int
           height:int
    }

, , , GameObject.

Selector.loadObject(gObject:IGameObject); 

, (, , , ), case, , , .

Tile, loadObject. GameOject. case , Tile, , .

, , , - , IGameObject. .

, , .

, , !

+3
3

, , , .

. , , , , .. . .

, . , , .

, , , Tree, Building House , , .., .

+1

, , - . . , GameObject GameObject; , . , , , "", , "", .

+3

Flyweight pattern

:

Flyweight is a software design template. Flies is an object that minimizes memory usage by sharing as much data as possible with other similar objects; this is a way to use objects in large quantities when a simple re-presentation will use an unacceptable amount of memory.

As for your second question, yes. We can say that all subclasses of the class implement all the interfaces that the parent class implements.

+2
source

Source: https://habr.com/ru/post/1726910/


All Articles