简体   繁体   中英

How to represent a unit as a point in pharo?

I am trying to program a game in Pharo. I have just started to code in Pharo, so I am trying to get used to it.
The game should include a game map and each character(or thing) is represented by a symbol.
The very first thing I did was to create a GameMapClass which contains a GameMap object.
Then I drew the map, which was easy.
The next thing to do is to put units. I have a Unit class(and some other classes that inherit the Unit class). I also have a unitAt method in my GameMapClass which is so far like this:

unitAt: aPoint |thePoint| ^thePoint := map at: aPoint ifAbsent: [self error: 'The space is empty'].

I don't know how to proceed in this method. The point should also returns the unit if it is not empty. How do I return for multiple times? Each point is aPointin my program. I also need to use dictionary to represent "things" with symbols and I have no idea about where to put this dictionary and how to access to it.

Here's a way to do it:

unitAt: aPoint
    | theUnit |
    theUnit := map 
        at: aPoint
        ifAbsent: [ self error: 'The space is empty' ].
    ^ { theUnit. aPoint }

Please consider @Uko's comment though: you pass in the point in this manner:

 unitAndPoint := myObject unitAt: myPoint.

If you look at this carefully you'll see that the variable unitAndPoint contains the same point object myPoint that you are passing as an argument. That means you already know the point and there is no reason for you to return it from unitAt: . Here's how I think the solution should (probably) look like:

unitAt: aPoint
    ^ map 
        at: aPoint
        ifAbsent: [ self error: 'The space is empty' ]

Called like this:

myPoint := 1@2.
unitOfPoint := myObject unitAt: myPoint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM