Good way to chop trees...

Hi,

I was thinking of a nice way to chop tree entities down. I want the bottom of the tree to tell all the parts of it to ‘fall’ when it is chopped down.

What do you think about the observer pattern for this?


class Tree  // Subject
{
     ArrayList<TreeEntity> observers = new ArrayList<>(); // observers

    public setState(int state) {
        this.state = state;
        notifyAllObservers();
    }
    public void attach(TreeEntity observer) {
         treeBlocks.add(observer);
    }
    public void notifyAllObservers() {
         for(TreeEntity observer : observers)
              observer.update();
   }
}

public abstract class Observer {
   protected Tree subject;
   public abstract void update();
}

public class TreeEntity extends Observer{

   public TreeEntity(Tree subject){
      this.subject = subject;
      this.subject.attach(this);
   }

   @Override
   public void update() {
       // fall, collapse or what not dependent on the state
   }
}

Thanks

You will bloat your memory with pointers to observers. The observer pattern is not meant to be used on highly granular subsystems. Also I see no damn point into separating a simple tree concept into two separate classes.

I guess if you just have N objects (the observers/listeners) which are associated with 1 other object (the subject/observable) and you just want the observable to call some method on all its observers in a list when something happens, you “could” call that the observer pattern.
But you don’t need to abstract away the type of the observers. That is, your observers are not ‘Observer’ things but concrete ‘TreeEntity’ (you even implemented it that way). And therefore you also don’t need that ‘Observer’ abstract class.
The observer pattern is useful when you want to keep the concrete type of the observers unknown/flexible and therefore would use a common base interface/abstract class for ‘Observer’ with a ‘notify()’ method (your update()), so that you would not care anymore about what kinds of concrete classes all implement that Observer interface/abstract class.
But in your case it is probably just a TreeEntity. So -> no abstraction needed.

Thanks for the comments.

So another way could be to when bottom of tree hit, remove, and remove any other tree blocks above it - this would need loops and evaluation…

Maybe having sprites for this be better way than how I’ve added them, that way, very simple to chop down and perform animation on it.