Implemented basic base menu to buy units

This commit is contained in:
Khopa 2019-07-06 13:21:03 +02:00
parent 6b5f77c415
commit bb32f47b8c
6 changed files with 136 additions and 25 deletions

View File

@ -16,4 +16,9 @@ QPushButton[style="btn-primary"]{
/*color:white;*/
/*padding: 21px 5px 21px 5px;
margin-top: 6px;*/
}
QBaseMenu{
background-color:#699245;
color:white;
}

View File

@ -19,10 +19,7 @@ class QTopPanel(QFrame):
def init_ui(self):
self.turnCounter = QTurnCounter()
self.turnCounter.setCurrentTurn(self.game.turn, self.game.current_day)
self.budgetBox = QBudgetBox()
self.budgetBox.setBudget(self.game.budget, self.game.budget_reward_amount)
self.passTurnButton = QPushButton("Pass Turn")
self.passTurnButton.setIcon(CONST.ICONS["PassTurn"])
@ -53,8 +50,9 @@ class QTopPanel(QFrame):
def setGame(self, game:Game):
self.game = game
self.turnCounter.setCurrentTurn(self.game.turn, self.game.current_day)
self.budgetBox.setBudget(self.game.budget, self.game.budget_reward_amount)
if game is not None:
self.turnCounter.setCurrentTurn(self.game.turn, self.game.current_day)
self.budgetBox.setBudget(self.game.budget, self.game.budget_reward_amount)
def openSettings(self):
QMessageBox.information(self, "Settings", "Todo open game settings")

View File

@ -57,7 +57,9 @@ class QLiberationMap(QGraphicsView):
def setGame(self, game: Game):
self.game = game
print("Reloading Map Canvas")
self.reload_scene()
print(self.game)
if self.game is not None:
self.reload_scene()
def reload_scene(self):
scene = self.scene()
@ -67,7 +69,7 @@ class QLiberationMap(QGraphicsView):
pos = self._transform_point(cp.position)
scene.addItem(QMapControlPoint(self, pos[0] - CONST.CP_SIZE / 2, pos[1] - CONST.CP_SIZE / 2, CONST.CP_SIZE,
CONST.CP_SIZE, cp))
CONST.CP_SIZE, cp, self.game))
# e = scene.addEllipse(pos[0]-CONST.CP_SIZE/2, pos[1]-CONST.CP_SIZE/2, CONST.CP_SIZE, CONST.CP_SIZE, QPen(brush=QBrush(color=color), width=5), brush=color)

View File

@ -5,14 +5,16 @@ from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsSceneHoverEvent, QGrap
from qt_ui.windows.QBaseMenu import QBaseMenu
from theater import ControlPoint
from game import Game
import qt_ui.uiconstants as CONST
class QMapControlPoint(QGraphicsRectItem):
def __init__(self, parent, x: float, y: float, w: float, h: float, model: ControlPoint):
def __init__(self, parent, x: float, y: float, w: float, h: float, model: ControlPoint, game: Game):
super(QMapControlPoint, self).__init__(x, y, w, h)
self.model = model
self.game = game
self.parent = parent
self.setAcceptHoverEvents(True)
self.setZValue(1)
@ -77,5 +79,5 @@ class QMapControlPoint(QGraphicsRectItem):
return self.model.captured and CONST.COLORS["bright_red"] or CONST.COLORS["dark_blue"]
def openBaseMenu(self):
window = QBaseMenu(self.window(), self.model)
window.show()
self.baseMenu = QBaseMenu(self.window(), self.model, self.game)
self.baseMenu.show()

View File

@ -1,23 +1,121 @@
from PySide2.QtCore import Qt
from PySide2.QtGui import QWindow
from PySide2.QtWidgets import QHBoxLayout, QLabel, QWidget
from PySide2.QtWidgets import QHBoxLayout, QLabel, QWidget, QFrame, QDialog, QVBoxLayout, QGridLayout, QPushButton
from dcs.unittype import UnitType
from theater import ControlPoint
from theater import ControlPoint, CAP, Embarking, AirDefence, CAS, PinpointStrike, db
from game import Game
class QBaseMenu(QWidget):
class QBaseMenu(QDialog):
def __init__(self, parent, controlPoint: ControlPoint):
def __init__(self, parent, controlPoint: ControlPoint, game: Game):
super(QBaseMenu, self).__init__(parent)
self.cp = controlPoint
self.game = game
self.deliveryEvent = self.game.units_delivery_event(self.cp)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.initUi()
def initUi(self):
layout = QHBoxLayout()
layout.addWidget(QLabel("TODO : This will be the base menu"))
layout.addWidget(QLabel(self.cp.name + " Base Info"))
central_widget = QWidget()
central_widget.setLayout(layout)
self.setLayout(layout)
self.setWindowTitle(self.cp.name)
self.topLayout = QHBoxLayout()
self.topLayout.setProperty("style", "baseMenuHeader")
self.topLayout.addWidget(QLabel("<b>" + self.cp.name + "</b>"))
self.topLayout.addWidget(
QLabel("{} / {} / {}".format(self.cp.base.total_planes, self.cp.base.total_armor, self.cp.base.total_aa)))
units = {
CAP: db.find_unittype(CAP, self.game.player_name),
Embarking: db.find_unittype(Embarking, self.game.player_name),
AirDefence: db.find_unittype(AirDefence, self.game.player_name),
CAS: db.find_unittype(CAS, self.game.player_name),
PinpointStrike: db.find_unittype(PinpointStrike, self.game.player_name),
}
self.mainLayout = QVBoxLayout()
self.mainLayout.addLayout(self.topLayout)
print(units)
tasks = list(units.keys())
tasks_per_column = 3
purchaseLayout = QGridLayout()
self.bought_amount_labels = {}
if not self.cp.captured:
column = 0
for i, tasks_column in [(i, tasks[idx:idx+tasks_per_column]) for i, idx in enumerate(range(0, len(tasks), tasks_per_column))]:
row = 2
def purchase_row(unit_type, unit_price):
layout = QHBoxLayout()
existing_units = self.cp.base.total_units_of_type(unit_type)
scheduled_units = self.deliveryEvent.units.get(unit_type, 0)
unitName = QLabel(db.unit_type_name(unit_type))
amountBought = QLabel("{} ({})".format(existing_units, scheduled_units))
self.bought_amount_labels[unit_type] = amountBought
price = QLabel("{}m".format(unit_price))
buy = QPushButton("+")
buy.clicked.connect(lambda: self.buy(unit_type))
sell = QPushButton("-")
sell.clicked.connect(lambda: self.sell(unit_type))
layout.addWidget(unitName)
layout.addWidget(amountBought)
layout.addWidget(price)
layout.addWidget(buy)
layout.addWidget(sell)
return layout
for task_type in tasks_column:
QLabel("<b>{}</b>".format(db.task_name(task_type)))
row += 1
units_column = list(set(units[task_type]))
units_column.sort(key=lambda x: db.PRICES[x])
for unit_type in units_column:
layout = purchase_row(unit_type, db.PRICES[unit_type])
self.mainLayout.addLayout(layout)
column += 5
self.setLayout(self.mainLayout)
def _update_count_label(self, unit_type: UnitType):
self.bought_amount_labels[unit_type].setText("({}{})".format(
self.cp.base.total_units_of_type(unit_type),
unit_type in self.deliveryEvent.units and ", bought {}".format(self.deliveryEvent.units[unit_type]) or ""
))
def buy(self, unit_type):
price = db.PRICES[unit_type]
if self.game.budget >= price:
self.deliveryEvent.deliver({unit_type: 1})
self.game.budget -= price
self._update_count_label(unit_type)
def sell(self, unit_type):
if self.deliveryEvent.units.get(unit_type, 0) > 0:
price = db.PRICES[unit_type]
self.game.budget += price
self.deliveryEvent.units[unit_type] = self.deliveryEvent.units[unit_type] - 1
if self.deliveryEvent.units[unit_type] == 0:
del self.deliveryEvent.units[unit_type]
elif self.cp.base.total_units_of_type(unit_type) > 0:
price = db.PRICES[unit_type]
self.game.budget += price
self.cp.base.commit_losses({unit_type: 1})
self._update_count_label(unit_type)

View File

@ -17,11 +17,12 @@ class QLiberationWindow(QMainWindow):
def __init__(self):
super(QLiberationWindow, self).__init__()
self.game = persistency.restore_game()
self.setGame(persistency.restore_game())
self.setGeometry(300, 100, 270, 100)
self.setWindowTitle("DCS Liberation")
self.setWindowIcon(QIcon("../resources/icon.png"))
self.setWindowIcon(QIcon("./resources/icon.png"))
self.statusBar().showMessage('Ready')
self.initUi()
@ -29,6 +30,8 @@ class QLiberationWindow(QMainWindow):
self.initMenuBar()
self.initToolbar()
self.connectSignals()
self.onGameGenerated(self.game)
def initUi(self):
@ -40,7 +43,8 @@ class QLiberationWindow(QMainWindow):
vbox = QVBoxLayout()
vbox.setMargin(0)
vbox.addWidget(QTopPanel(self.game))
vbox.addLayout(hbox)
vbox.addWidget(self.liberation_map)
#vbox.addLayout(hbox)
central_widget = QWidget()
central_widget.setLayout(vbox)
@ -113,14 +117,16 @@ class QLiberationWindow(QMainWindow):
def newGame(self):
wizard = NewGameWizard(self)
wizard.show()
wizard.accepted.connect(lambda: self.setGame(wizard.generatedGame))
wizard.accepted.connect(lambda: self.onGameGenerated(wizard.generatedGame))
def saveGame(self):
print("Saving game")
persistency.save_game(self.game)
GameUpdateSignal.get_instance().updateGame(self.game)
def onGameGenerated(self):
def onGameGenerated(self, game: Game):
print("On Game generated")
self.game = game
GameUpdateSignal.get_instance().updateGame(self.game)
def setGame(self, game: Game):