I am very new to Nim, and am trying to port the libtcod tutorial written in 
Python - here: [Complete Roguelike Tutorial, using 
python+libtcod](http://www.roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python%2Blibtcod)
 \- to Nim.

For the most part, it is smooth sailing, but now we are going to use 
composition and inheritance, and this is what I've got:
    
    
    type
      Fighter = ref object of RootObj
        max_hp, hp, defense, power : int
        owner : ref RootObj
      
      AI = ref object of RootObj
        owner : ref RootObj
      
      # Generic object represented by a character on the screen
      # A Thing can be: player, monster, item, stairs, ...
      Thing = ref object of RootObj
        x, y : int
        color : TColor
        symbol : char
        name : string
        blocks : bool
        fighter : Fighter
        ai : AI
      
      BasicMonster = ref object of AI
    

Then, the Thing constructor:
    
    
    proc newThing(x : int, y : int, symbol : char, name : string, color : 
TColor, blocks : bool, fighter : Fighter = nil, ai : AI = nil) : Thing =
      result = new Thing
      result.x = x
      result.y = y
      result.symbol = symbol
      result.name = name
      result.color = color
      result.blocks = blocks
      result.fighter = fighter
      if fighter != nil:
        fighter.owner = result
        result.ai = ai
      if ai != nil:
        ai.owner = result
    

And an example of a method using it:
    
    
    method take_turn(self : BasicMonster) =
      var monster = Thing(self.owner)
      if map_is_in_fov(fov_map, monster.x, monster.y):
        if monster.distance_to(player) >= 2:
          monster.move_towards(player.x, player.y)
        elif player.fighter.hp > 0:
          echo("The attack of the ", monster.name, " bounces off your shiny 
metal armor!")
    

While it seems to work, it does look a bit _messy_ \- am I on the right track?

The whole idea behind the AI component is that it can be of different types - 
_BasicMonster_ is just one of them, using a dead simple terminator chase 
approach.

Reply via email to