mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
game loop; player budget; enemy progression; GUI WIP
This commit is contained in:
committed by
Vasiliy Horbachenko
parent
4cd3c24b49
commit
ad4d183972
0
ui/__init__.py
Normal file
0
ui/__init__.py
Normal file
62
ui/basemenu.py
Normal file
62
ui/basemenu.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from shop import db
|
||||
|
||||
from tkinter import *
|
||||
from ui.window import *
|
||||
from ui.eventmenu import *
|
||||
|
||||
from game.game import *
|
||||
|
||||
|
||||
class BaseMenu:
|
||||
def __init__(self, window: Window, parent, game: Game, base: Base):
|
||||
self.window = window
|
||||
self.frame = window.right_pane
|
||||
self.parent = parent
|
||||
self.game = game
|
||||
self.base = base
|
||||
|
||||
self.update()
|
||||
|
||||
def go_back(self):
|
||||
self.parent.update()
|
||||
|
||||
def buy(self, unit_type):
|
||||
def action():
|
||||
price = db.PRICES[unit_type]
|
||||
if self.game.budget > price:
|
||||
self.base.commision_units({unit_type: 1})
|
||||
self.game.budget -= price
|
||||
|
||||
self.update()
|
||||
|
||||
return action
|
||||
|
||||
def update(self):
|
||||
self.window.clear_right_pane()
|
||||
row = 0
|
||||
|
||||
def purchase_row(unit_type, unit_price):
|
||||
nonlocal row
|
||||
|
||||
existing_units = self.base.total_units_of_type(unit_type)
|
||||
Label(self.frame, text=db.unit_type_name(unit_type)).grid(column=0, row=row, sticky=W)
|
||||
Label(self.frame, text="{}m {}".format(unit_price, existing_units)).grid(column=1, row=row)
|
||||
Button(self.frame, text="Buy", command=self.buy(unit_type)).grid(column=2, row=row)
|
||||
row += 1
|
||||
|
||||
units = {
|
||||
CAP: db.find_unittype(CAP, self.game.player),
|
||||
CAS: db.find_unittype(CAS, self.game.player),
|
||||
FighterSweep: db.find_unittype(FighterSweep, self.game.player),
|
||||
AirDefence: db.find_unittype(AirDefence, self.game.player),
|
||||
}
|
||||
|
||||
Label(self.frame, text="Budget: {}m".format(self.game.budget)).grid(column=0, row=row, sticky=W)
|
||||
Button(self.frame, text="Back", command=self.go_back).grid(column=2, row=row)
|
||||
row += 1
|
||||
|
||||
for task_type, units in units.items():
|
||||
Label(self.frame, text="{}".format(db.task_name(task_type))).grid(column=0, row=row, columnspan=3); row += 1
|
||||
for unit_type in units:
|
||||
purchase_row(unit_type, db.PRICES[unit_type])
|
||||
|
||||
98
ui/eventmenu.py
Normal file
98
ui/eventmenu.py
Normal file
@@ -0,0 +1,98 @@
|
||||
from tkinter import *
|
||||
from ui.window import *
|
||||
from ui.eventresultsmenu import *
|
||||
|
||||
from game.game import *
|
||||
from game import event
|
||||
|
||||
|
||||
class EventMenu:
|
||||
aircraft_scramble_entries = None # type: typing.Dict[PlaneType, Entry]
|
||||
armor_scramble_entries = None # type: typing.Dict[Armor, Entry]
|
||||
|
||||
def __init__(self, window: Window, parent, game: Game, event: event.Event):
|
||||
self.window = window
|
||||
self.frame = self.window.right_pane
|
||||
self.parent = parent
|
||||
|
||||
self.event = event
|
||||
self.game = game
|
||||
|
||||
self.aircraft_scramble_entries = {}
|
||||
self.armor_scramble_entries = {}
|
||||
|
||||
self.update()
|
||||
|
||||
def start(self):
|
||||
scrambled_aircraft = {}
|
||||
scrambled_sweep = {}
|
||||
scrambled_cas = {}
|
||||
for unit_type, field in self.aircraft_scramble_entries.items():
|
||||
value = field.get()
|
||||
if value and int(value) > 0:
|
||||
amount = int(value)
|
||||
task = db.unit_task(unit_type)
|
||||
|
||||
scrambled_aircraft[unit_type] = amount
|
||||
if task == CAS:
|
||||
scrambled_cas[unit_type] = amount
|
||||
elif task == FighterSweep:
|
||||
scrambled_sweep[unit_type] = amount
|
||||
|
||||
scrambled_armor = {}
|
||||
for unit_type, field in self.armor_scramble_entries.items():
|
||||
value = field.get()
|
||||
if value and int(value) > 0:
|
||||
scrambled_armor[unit_type] = int(value)
|
||||
|
||||
if type(self.event) is CaptureEvent:
|
||||
e = self.event # type: CaptureEvent
|
||||
if self.game.is_player_attack(self.event):
|
||||
e.player_attacking(cas=scrambled_cas,
|
||||
escort=scrambled_sweep,
|
||||
armor=scrambled_armor)
|
||||
else:
|
||||
e.player_defending(interceptors=scrambled_aircraft)
|
||||
elif type(self.event) is InterceptEvent:
|
||||
e = self.event # type: InterceptEvent
|
||||
if self.game.is_player_attack(self.event):
|
||||
e.player_attacking(interceptors=scrambled_aircraft)
|
||||
else:
|
||||
e.player_defending(escort=scrambled_aircraft)
|
||||
elif type(self.event) is GroundInterceptEvent:
|
||||
e = self.event # type: GroundInterceptEvent
|
||||
e.player_attacking(e.to_cp.position.random_point_within(30000), strikegroup=scrambled_aircraft)
|
||||
|
||||
self.game.initiate_event(self.event)
|
||||
EventResultsMenu(self.window, self.parent, self.game, self.event)
|
||||
|
||||
def update(self):
|
||||
self.window.clear_right_pane()
|
||||
row = 0
|
||||
|
||||
def label(text):
|
||||
nonlocal row
|
||||
Label(self.frame, text=text).grid(column=0, row=0)
|
||||
|
||||
row += 1
|
||||
|
||||
def scrable_row(unit_type, unit_count):
|
||||
nonlocal row
|
||||
Label(self.frame, text="{} ({})".format(unit_type.id and unit_type.id or unit_type.name, unit_count)).grid(column=0, row=row)
|
||||
e = Entry(self.frame)
|
||||
e.grid(column=1, row=row)
|
||||
|
||||
self.aircraft_scramble_entries[unit_type] = e
|
||||
row += 1
|
||||
|
||||
base = None # type: Base
|
||||
if self.event.attacker.name == self.game.player:
|
||||
base = self.event.from_cp.base
|
||||
else:
|
||||
base = self.event.to_cp.base
|
||||
|
||||
label("Aircraft")
|
||||
for unit, count in base.aircraft.items():
|
||||
scrable_row(unit, count)
|
||||
|
||||
Button(self.frame, text="Commit", command=self.start).grid(column=0, row=row)
|
||||
61
ui/eventresultsmenu.py
Normal file
61
ui/eventresultsmenu.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import math
|
||||
|
||||
from tkinter import *
|
||||
from ui.window import *
|
||||
|
||||
from userdata.debriefing_parser import *
|
||||
from game.game import *
|
||||
from game import event
|
||||
|
||||
|
||||
class EventResultsMenu:
|
||||
def __init__(self, window: Window, parent, game: Game, event: Event):
|
||||
self.window = window
|
||||
self.frame = window.right_pane
|
||||
self.parent = parent
|
||||
|
||||
self.game = game
|
||||
self.event = event
|
||||
|
||||
self.update()
|
||||
|
||||
def simulate_result(self, player_factor: float, enemy_factor: float, result: bool):
|
||||
def action():
|
||||
debriefing = Debriefing()
|
||||
|
||||
def count_planes(groups: typing.List[FlyingGroup], mult: float) -> typing.Dict[UnitType, int]:
|
||||
result = {}
|
||||
for group in groups:
|
||||
for unit in group.units:
|
||||
result[unit.type] = result.get(unit.type, 0) + 1 * mult
|
||||
|
||||
return {x: math.floor(y) for x, y in result.items()}
|
||||
|
||||
player_planes = self.event.operation.mission.country(self.game.player).plane_group
|
||||
enemy_planes = self.event.operation.mission.country(self.game.enemy).plane_group
|
||||
|
||||
player_losses = count_planes(player_planes, player_factor)
|
||||
enemy_losses = count_planes(enemy_planes, enemy_factor)
|
||||
|
||||
debriefing.destroyed_units = {
|
||||
self.game.player: player_losses,
|
||||
self.game.enemy: enemy_losses,
|
||||
}
|
||||
|
||||
self.game.finish_event(self.event, debriefing)
|
||||
self.game.pass_turn()
|
||||
self.parent.update()
|
||||
|
||||
return action
|
||||
|
||||
def update(self):
|
||||
self.window.clear_right_pane()
|
||||
|
||||
Button(self.frame, text="no losses, succ", command=self.simulate_result(0, 1, True)).grid(row=0, column=0)
|
||||
Button(self.frame, text="no losses, fail", command=self.simulate_result(0, 1, False)).grid(row=0, column=1)
|
||||
|
||||
Button(self.frame, text="half losses, succ", command=self.simulate_result(0.5, 0.5, True)).grid(row=1, column=0)
|
||||
Button(self.frame, text="half losses, fail", command=self.simulate_result(0.5, 0.5, False)).grid(row=1, column=1)
|
||||
|
||||
Button(self.frame, text="full losses, succ", command=self.simulate_result(1, 0, True)).grid(row=2, column=0)
|
||||
Button(self.frame, text="full losses, fail", command=self.simulate_result(1, 0, False)).grid(row=2, column=1)
|
||||
78
ui/mainmenu.py
Normal file
78
ui/mainmenu.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from tkinter import *
|
||||
from tkinter.ttk import *
|
||||
|
||||
from ui.window import *
|
||||
from ui.eventmenu import *
|
||||
from ui.basemenu import *
|
||||
|
||||
from game.game import *
|
||||
|
||||
class MainMenu:
|
||||
def __init__(self, game: Game, window: Window):
|
||||
self.image = PhotoImage(file="resources/caumap.gif")
|
||||
self.game = game
|
||||
self.window = window
|
||||
|
||||
map = Label(window.left_pane, image=self.image)
|
||||
map.grid(column=0, row=0)
|
||||
|
||||
self.frame = self.window.right_pane
|
||||
self.frame.grid_columnconfigure(0, weight=1)
|
||||
self.update()
|
||||
|
||||
def pass_turn(self):
|
||||
self.game.pass_turn()
|
||||
self.update()
|
||||
|
||||
def start_event(self, event) -> typing.Callable:
|
||||
return lambda: EventMenu(self.window, self, self.game, event)
|
||||
|
||||
def go_cp(self, cp: ControlPoint) -> typing.Callable:
|
||||
return lambda: BaseMenu(self.window, self, self.game, cp.base)
|
||||
|
||||
def update(self):
|
||||
self.window.clear_right_pane()
|
||||
|
||||
row = 1
|
||||
|
||||
def label(text):
|
||||
nonlocal row
|
||||
Label(self.frame, text=text).grid(column=0, row=row, sticky=NW)
|
||||
row += 1
|
||||
|
||||
def event_button(event, text):
|
||||
nonlocal row
|
||||
Button(self.frame, text=text, command=self.start_event(event)).grid(column=0, row=row, sticky=N)
|
||||
row += 1
|
||||
|
||||
def cp_button(cp):
|
||||
nonlocal row
|
||||
title = "{}{}{}{}".format(
|
||||
cp.name,
|
||||
"^" * cp.base.total_planes,
|
||||
"." * cp.base.total_armor,
|
||||
"*" * cp.base.total_aa)
|
||||
Button(self.frame, text=title, command=self.go_cp(cp)).grid(column=0, row=row, sticky=NW)
|
||||
row += 1
|
||||
|
||||
Button(self.frame, text="Pass turn", command=self.pass_turn).grid(column=0, row=row, sticky=N); row += 1
|
||||
label("Budget: {}m".format(self.game.budget))
|
||||
|
||||
for event in self.game.events:
|
||||
event_button(event, "{} {}".format(event.attacker.name != self.game.player and "!" or " ", event))
|
||||
|
||||
Separator(self.frame, orient='horizontal').grid(column=0, row=row, sticky=EW); row += 1
|
||||
for cp in self.game.theater.player_points():
|
||||
cp_button(cp)
|
||||
|
||||
Separator(self.frame, orient='horizontal').grid(column=0, row=row, sticky=EW); row += 1
|
||||
for cp in self.game.theater.enemy_bases():
|
||||
title = "[{}] {}{}{}{}".format(
|
||||
int(cp.base.strength * 10),
|
||||
cp.name,
|
||||
"^" * cp.base.total_planes,
|
||||
"." * cp.base.total_armor,
|
||||
"*" * cp.base.total_aa)
|
||||
Label(self.frame, text=title).grid(column=0, row=row, sticky=NE)
|
||||
row += 1
|
||||
|
||||
41
ui/window.py
Normal file
41
ui/window.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from tkinter import *
|
||||
|
||||
|
||||
class Window:
|
||||
image = None
|
||||
left_pane = None # type: Frame
|
||||
right_pane = None # type: Frame
|
||||
|
||||
def __init__(self):
|
||||
self.tk = Tk()
|
||||
self.tk.grid_columnconfigure(0, weight=1)
|
||||
self.tk.grid_rowconfigure(0, weight=1)
|
||||
|
||||
self.frame = Frame(self.tk)
|
||||
self.frame.grid(column=0, row=0, sticky=NSEW)
|
||||
self.frame.grid_columnconfigure(0, minsize=300)
|
||||
self.frame.grid_columnconfigure(1, minsize=300)
|
||||
|
||||
self.frame.grid_columnconfigure(0, weight=0)
|
||||
self.frame.grid_columnconfigure(1, weight=1)
|
||||
self.frame.grid_rowconfigure(0, weight=1)
|
||||
|
||||
self.left_pane = Frame(self.frame)
|
||||
self.left_pane.grid(column=0, row=0, sticky=NSEW)
|
||||
self.right_pane = Frame(self.frame)
|
||||
self.right_pane.grid(column=1, row=0, sticky=NSEW)
|
||||
|
||||
self.tk.focus()
|
||||
|
||||
def clear_right_pane(self):
|
||||
for x in self.right_pane.winfo_children():
|
||||
x.grid_remove()
|
||||
|
||||
def clear(self):
|
||||
for x in self.left_pane.winfo_children():
|
||||
x.grid_remove()
|
||||
for x in self.right_pane.winfo_children():
|
||||
x.grid_remove()
|
||||
|
||||
def run(self):
|
||||
self.tk.mainloop()
|
||||
Reference in New Issue
Block a user