roguev6 > charcter.py


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

変更点

  • ダメージ計算メソッドがアイテムや他のキャラクタとのパラメータの絡みが多くなりすぎたので他のクラスへ譲渡。

追加すべき点

  • ObjectPool#Encounter(pos:Vector2d,enemy:str):void
#coding:shift-jis
import system
from vect import Vector2d,Direction
from action import ActType

class Charcter:
  def __init__(self,pos,charcter_param,map_name='no_name',direction=Direction.SOUTH):
    self.pos=pos.copy()
    self.direction = direction
    self.map_name = map_name 
    self.param = charcter_param
    self.hungry=100
    self.hp = self.param.maxhp
    def non_func(victimizer,victim,damage):
      return damage
    self.equiped ={
        'weapon_handle':non_func,
        #参照,効果を付加させるだけなので、ハンドル登録でもいいかも。
        'shield_handle':non_func}
  def __item__(self,key):
    if key in self.tag:
      return self.tag[key]
    else:
      return 0
  def think(self,obj_pool,stages,items):
    raise Exception('override Charcter#think method.')
  def reposition(self,map_name,vec2d):
    self.pos = vec2d.copy()
    self.map_name=map_name
  def move(self,direction):
    self.pos+=direction

class ConcreteCharcter(Charcter):
  def think(self,obj_pool,stages,items):
    return (ActType.REST,Direction.NO_DIR)

class ObjPool:
  MAXPOOL = system.GAME_OBJECT_NUM
  def __init__(self):
    self.pool=[]
  def __len__(self):
    return len(self.pool)
  def __getitem__(self,id):
    if len(self.pool) <= id:
      raise Exception(u'ObjPoolのインデックスが限界を超えました')
    return self.pool[id]
  def append(self,item):
    self.pool.append(item)
  def get_at(self,pos):
    for obj in self.pool:
      if obj.get_pos().eq(pos):
        return obj
    return None
  def __iter__(self):
    return (item for item in self.pool)
  def get_player(self):
    for item in self.pool:
      if isinstance(item,Player):return item
    raise Exception(u'ObjPoolにPlayerデータが格納されていない')
  #条件にあったオブジェクトを返す
  def get_cond(self,cond):
    return [item for item in self.pool if cond(item)]

def test_code():
  import system
  conc = ConcreteCharcter(Vector2d(0,0), system.make_param('player'), 0)
  conc.move(Direction.NORTH)
  assert conc.pos == Vector2d(0,-1)
  conc.reposition('forest',Vector2d(32,20))
  assert conc.pos == Vector2d(32,20)
  assert conc.map_name== 'forest'
  assert conc.think(None,None,None) == ('act_rest',Direction.NO_DIR)

  #中を弄るときはtest_codeを書く。
  pool = ObjPool()

if __name__=='__main__':
  test_code()