province_info.py 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import cocos
  2. from cocos.director import director
  3. from app.verkolst import verkolst
  4. class ProvinceInfo(cocos.layer.Layer):
  5. is_event_handler = False
  6. def __init__(self):
  7. super().__init__()
  8. self.position = (director.window.width+450, director.window.height)
  9. self.province = None
  10. background = cocos.sprite.Sprite('province_info_bg.png', position=(0, 0), anchor=(450, 300))
  11. self.add(background)
  12. self.terrain_type_label = cocos.text.Label("-", font_size=10, position=(-450+30, -30))
  13. self.add(self.terrain_type_label)
  14. self.lc_pops_label = cocos.text.HTMLLabel("-", position=(-450+30, -45))
  15. self.add(self.lc_pops_label)
  16. self.mc_pops_label = cocos.text.HTMLLabel("-", position=(-450+30, -60))
  17. self.add(self.mc_pops_label)
  18. self.uc_pops_label = cocos.text.HTMLLabel("-", position=(-450+30, -75))
  19. self.add(self.uc_pops_label)
  20. self.food_production_label = cocos.text.Label("-", font_size=10, position=(-450+30, -90))
  21. self.add(self.food_production_label)
  22. self.food_consumption_label = cocos.text.Label("-", font_size=10, position=(-450+30, -105))
  23. self.add(self.food_consumption_label)
  24. self.food_rate_label = cocos.text.Label("-", font_size=10, position=(-450+30, -120))
  25. self.add(self.food_rate_label)
  26. self.food_stored_label = cocos.text.Label("-", font_size=10, position=(-450+30, -135))
  27. self.add(self.food_stored_label)
  28. self.trade_value_label = cocos.text.Label("-", font_size=10, position=(-450+30, -150))
  29. self.add(self.trade_value_label)
  30. self.export_partner_label = cocos.text.Label("-", font_size=10, position=(-450+30, -165))
  31. self.add(self.export_partner_label)
  32. def set_province(self, province):
  33. if self.province is None and province is not None:
  34. self.do(cocos.actions.MoveTo((director.window.width, director.window.height), 0.1))
  35. elif self.province is not None and province is None:
  36. self.do(cocos.actions.MoveTo((director.window.width+450, director.window.height), 0.1))
  37. self.province = province
  38. self._render()
  39. def _render(self):
  40. if self.province is not None:
  41. self.terrain_type_label.element.text = (f'{self.province.tile.properties["name"]}'
  42. f' ({self.province.i}, {self.province.j})')
  43. i, j = self.province.i, self.province.j
  44. resources = verkolst.resources
  45. pops_tot = resources.lc_pops[i][j] + resources.mc_pops[i][j] + resources.uc_pops[i][j]
  46. def _fmt(label, number, starving):
  47. if pops_tot > 0:
  48. if starving[i][j]:
  49. hl_color = "#B22222" # Red
  50. elif resources.any_starvation[i][j]:
  51. hl_color = "#DAA520" # Yellow
  52. else:
  53. hl_color = "#FFFFF0" # Off White
  54. return (f'<font color="#FFFFF0">{label}: </font><font color="{hl_color}">{int(number[i][j]):d}</font>'
  55. f'<font color="#FFFFF0"> - {100*number[i][j]/pops_tot:.1f}%</font>'
  56. )
  57. else:
  58. return f'<font color="#FFFFF0">{label}: - </font>'
  59. self.lc_pops_label.element.text = _fmt('LC', resources.lc_pops, resources.lc_starvation)
  60. self.mc_pops_label.element.text = _fmt('MC', resources.mc_pops, resources.mc_starvation)
  61. self.uc_pops_label.element.text = _fmt('UC', resources.uc_pops, resources.uc_starvation)
  62. self.food_production_label.element.text = f"Food production: {resources.food_production[i][j]:.1f}"
  63. self.food_consumption_label.element.text = f"Food consumption: {resources.food_consumption[i][j]:.1f}"
  64. self.food_rate_label.element.text = f"Food production rate: {resources.food_rate[i][j]:.1f}"
  65. self.food_stored_label.element.text = f"Food stored: {resources.food_stored[i][j]:.1f}"
  66. self.trade_value_label.element.text = f"Trade value: {resources.trade_value[i][j]:.1f}"
  67. self.export_partner_label.element.text = f"Exporting to: ({resources.export_partner[i][j][0]:.1f}, {resources.export_partner[i][j][1]:.1f})"
  68. def on_turn_start(self):
  69. self._render()