绘制 table 多种类型

Draw table of multiple types

我有两个僵尸对象,一个是Grunt,一个是Runner

我的 Grunt.lua 文件:

function InitGrunt()
  grunt = {}
  grunt.x = 0
  grunt.y = 0
  grunt.speed = 120
  grunt.hitBox = (sprites.grunt:getHeight() + sprites.grunt:getWidth())/2
  grunt.hit = false

  gruntDefCD = 2
  gruntCD = gruntDefCD
  gruntMinCD = 0.4
  gruntTimer = gruntCD
  gruntTimerDecr = 0.8
end

function SpawnGrunt()
  local side = math.random(1, 4)

  --randomize spawn position

  table.insert( zombies, grunt )
end

和我的 Runner.lua:

function InitRunner()
  runner = {}
  runner.x = 0
  runner.y = 0
  runner.speed = 240
  runner.hitBox = (sprites.runner:getWidth() + sprites.runner:getHeight())/2
  runner.hit = false

  runnerDefCD = 4
  runnerCD = runnerDefCD
  runnerMinCD = 2
  runnerTimer = runnerCD
  runnerTimerDecr = 0.95
end

function SpawnRunner()
      local side = math.random(1, 4)

      --randomize spawn position

      table.insert( zombies, runner )
end

所以 zombie table 将有 gruntrunner。如何在 Draw()?

中打印它们

我可以画一个:

function DrawGrunts()
  for i, z in ipairs(zombies) do
    love.graphics.draw(sprites.grunt, z.x, z.y, PlayerZombieAngle(z), nil, nil, sprites.grunt:getWidth()/2, sprites.grunt:getHeight()/2 )
  end
end

但是我如何在一个函数中绘制它们,理想情况下?

给不同的实体一个指向它们的精灵对象的指针:

runner = { }
runner.x = 0
runner.y = 0
runner.sprite = sprites.runner
-- Rest of runner definition

grunt = { } 
grunt.x = 0 
grunt.y = 0
grunt.sprite = sprites.grunt
-- Rest of grunt definition

并且您的绘制函数变为:

for i, z in ipairs(zombies) do
    love.graphics.draw(z.sprite, z.x, z.y, ...)
end