game_objects.nim 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import chipmunk, sfml, animations, sg_assets
  2. type
  3. PGameObject* = ref TGameObject
  4. TGameObject = object
  5. body*: chipmunk.PBody
  6. shape*: chipmunk.PShape
  7. record*: PObjectRecord
  8. anim*: PAnimation
  9. proc `$`*(obj: PGameObject): string =
  10. result = "<Object "
  11. result.add obj.record.name
  12. result.add ' '
  13. result.add($obj.body.getpos())
  14. result.add '>'
  15. proc free(obj: PGameObject) =
  16. obj.record = nil
  17. free(obj.anim)
  18. obj.anim = nil
  19. proc newObject*(record: PObjectRecord): PGameObject =
  20. if record.isNil: return nil
  21. new(result, free)
  22. result.record = record
  23. result.anim = newAnimation(record.anim, AnimLoop)
  24. when false:
  25. result.sprite = record.anim.spriteSheet.sprite.copy()
  26. result.body = newBody(result.record.physics.mass, 10.0)
  27. result.shape = chipmunk.newCircleShape(result.body, result.record.physics.radius, VectorZero)
  28. result.body.setPos(vector(100, 100))
  29. proc newObject*(name: string): PGameObject =
  30. result = newObject(fetchObj(name))
  31. proc draw*(window: PRenderWindow, obj: PGameObject) {.inline.} =
  32. window.draw(obj.anim.sprite)