2021-09-08 23:38:34 +08:00
|
|
|
# -------------------------------
|
|
|
|
# Difficult Rocket
|
2021-12-26 23:06:03 +08:00
|
|
|
# Copyright © 2021-2022 by shenjackyuanjie
|
2021-09-08 23:38:34 +08:00
|
|
|
# All rights reserved
|
|
|
|
# -------------------------------
|
|
|
|
|
2021-02-20 21:46:14 +08:00
|
|
|
"""
|
2020-12-13 18:39:12 +08:00
|
|
|
writen by shenjackyuanjie
|
2021-09-08 23:38:34 +08:00
|
|
|
mail: 3695888@qq.com
|
|
|
|
github: @shenjackyuanjie
|
|
|
|
gitee: @shenjackyuanjie
|
2021-02-20 21:46:14 +08:00
|
|
|
"""
|
2021-09-08 23:38:34 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
# system function
|
2021-02-20 21:46:14 +08:00
|
|
|
import os
|
2021-03-25 23:15:18 +08:00
|
|
|
import sys
|
2021-04-10 22:51:08 +08:00
|
|
|
import time
|
2022-05-25 09:16:38 +08:00
|
|
|
import ctypes
|
2021-09-22 06:21:48 +08:00
|
|
|
import logging
|
2021-10-25 22:08:00 +08:00
|
|
|
import traceback
|
2021-09-22 06:21:48 +08:00
|
|
|
|
2021-09-24 00:04:56 +08:00
|
|
|
from decimal import Decimal
|
2021-08-24 22:31:52 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
# Difficult_Rocket function
|
2022-05-11 11:11:39 +08:00
|
|
|
from Difficult_Rocket.command import line, tree
|
|
|
|
from Difficult_Rocket.api import new_thread
|
2022-04-26 22:05:58 +08:00
|
|
|
from Difficult_Rocket.utils.translate import tr
|
2021-11-06 19:07:32 +08:00
|
|
|
from Difficult_Rocket.guis.widgets import InputBox
|
2022-05-25 09:16:38 +08:00
|
|
|
# from Difficult_Rocket.client.screen import DRScreen
|
2022-04-26 22:05:58 +08:00
|
|
|
from Difficult_Rocket.utils import tools, translate
|
2022-05-25 09:16:38 +08:00
|
|
|
from Difficult_Rocket.api.Exp.command import CommandError
|
2022-03-22 21:18:04 +08:00
|
|
|
from Difficult_Rocket.client.fps.fps_log import FpsLogger
|
2021-02-03 22:04:34 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
# libs function
|
|
|
|
local_lib = True
|
|
|
|
if local_lib:
|
|
|
|
from libs import pyglet
|
2021-11-06 19:07:32 +08:00
|
|
|
from libs.pyglet.window import Window
|
2021-09-23 06:34:23 +08:00
|
|
|
from libs.pyglet.window import key, mouse
|
2022-03-22 21:18:04 +08:00
|
|
|
from libs import toml
|
2021-09-23 06:34:23 +08:00
|
|
|
else:
|
|
|
|
import pyglet
|
|
|
|
from pyglet.window import key, mouse
|
2022-03-22 21:18:04 +08:00
|
|
|
import toml
|
2021-09-23 06:34:23 +08:00
|
|
|
|
2020-12-16 06:33:33 +08:00
|
|
|
|
2021-08-13 12:25:29 +08:00
|
|
|
class Client:
|
2021-09-22 06:21:48 +08:00
|
|
|
def __init__(self, net_mode='local'):
|
2021-10-01 23:12:01 +08:00
|
|
|
start_time = time.time_ns()
|
2021-02-16 22:28:13 +08:00
|
|
|
# logging
|
2021-08-13 12:25:29 +08:00
|
|
|
self.logger = logging.getLogger('client')
|
|
|
|
# config
|
2022-03-22 21:18:04 +08:00
|
|
|
self.config = tools.load_file('./configs/main.toml')
|
2021-02-16 22:28:13 +08:00
|
|
|
# value
|
|
|
|
self.process_id = 'Client'
|
|
|
|
self.process_name = 'Client process'
|
2021-02-20 21:46:14 +08:00
|
|
|
self.process_pid = os.getpid()
|
2021-02-16 22:28:13 +08:00
|
|
|
self.net_mode = net_mode
|
2021-08-13 12:25:29 +08:00
|
|
|
self.caption = tools.name_handler(self.config['window']['caption'], {'version': self.config['runtime']['version']})
|
2021-09-22 06:21:48 +08:00
|
|
|
self.window = ClientWindow(net_mode=self.net_mode,
|
2021-08-13 12:25:29 +08:00
|
|
|
width=int(self.config['window']['width']),
|
|
|
|
height=int(self.config['window']['height']),
|
|
|
|
fullscreen=tools.format_bool(self.config['window']['full_screen']),
|
|
|
|
caption=self.caption,
|
|
|
|
resizable=tools.format_bool(self.config['window']['resizable']),
|
|
|
|
visible=tools.format_bool(self.config['window']['visible']))
|
2021-10-01 23:12:01 +08:00
|
|
|
self.logger.info(tr.lang('client', 'setup.done'))
|
|
|
|
end_time = time.time_ns()
|
|
|
|
self.use_time = end_time - start_time
|
|
|
|
self.logger.info(tr.lang('client', 'setup.use_time').format(Decimal(self.use_time) / 1000000000))
|
|
|
|
self.logger.debug(tr.lang('client', 'setup.use_time_ns').format(self.use_time))
|
2021-02-20 21:46:14 +08:00
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
def start(self):
|
|
|
|
self.window.start_game() # 游戏启动
|
2021-08-24 22:31:52 +08:00
|
|
|
# TODO 写一下服务端启动相关,还是需要服务端啊
|
2021-01-11 21:58:51 +08:00
|
|
|
|
2021-02-16 22:28:13 +08:00
|
|
|
|
2022-05-11 11:11:39 +08:00
|
|
|
def pyglet_load_fonts_folder(folder) -> None:
|
|
|
|
file_folder_list = os.listdir(folder)
|
|
|
|
for obj in file_folder_list:
|
|
|
|
if os.path.isfile(os.path.join(folder, obj)):
|
|
|
|
if obj[-4:] == '.ttf':
|
|
|
|
pyglet.font.add_file(os.path.join(folder, obj))
|
|
|
|
else:
|
|
|
|
pyglet_load_fonts_folder(os.path.join(folder, obj))
|
|
|
|
|
|
|
|
|
2021-11-06 19:07:32 +08:00
|
|
|
class ClientWindow(Window):
|
2021-02-16 22:28:13 +08:00
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
def __init__(self, net_mode='local', *args, **kwargs):
|
2021-10-01 23:12:01 +08:00
|
|
|
start_time = time.time_ns()
|
2021-08-13 12:25:29 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2021-02-16 12:51:07 +08:00
|
|
|
"""
|
2021-02-03 22:04:34 +08:00
|
|
|
:param dev_list: 共享内存
|
|
|
|
:param dev_dic: 共享内存
|
|
|
|
:param logger: logger
|
|
|
|
:param net_mode: 网络模式 # local / ip
|
2021-02-16 12:51:07 +08:00
|
|
|
"""
|
2021-01-25 12:22:55 +08:00
|
|
|
# logging
|
2021-08-13 12:25:29 +08:00
|
|
|
self.logger = logging.getLogger('client')
|
2021-02-03 22:04:34 +08:00
|
|
|
# value
|
|
|
|
self.net_mode = net_mode
|
2021-09-22 06:21:48 +08:00
|
|
|
self.run_input = False
|
2021-08-24 22:31:52 +08:00
|
|
|
# configs
|
2022-02-07 22:14:51 +08:00
|
|
|
pyglet.resource.path = ['/textures/']
|
2021-08-24 22:31:52 +08:00
|
|
|
pyglet.resource.reindex()
|
2022-03-22 21:18:04 +08:00
|
|
|
self.set_icon(pyglet.image.load('./textures/icon.png'))
|
|
|
|
self.main_config = tools.load_file('./configs/main.toml')
|
|
|
|
self.game_config = tools.load_file('./configs/game.config')
|
2021-09-02 22:47:10 +08:00
|
|
|
# FPS
|
2021-11-06 19:07:32 +08:00
|
|
|
self.FPS = Decimal(int(self.main_config['runtime']['fps']))
|
2021-10-28 06:43:35 +08:00
|
|
|
self.SPF = Decimal('1') / self.FPS
|
2021-12-15 23:28:08 +08:00
|
|
|
self.fps_log = FpsLogger(stable_fps=int(self.FPS))
|
2021-02-20 21:46:14 +08:00
|
|
|
# batch
|
|
|
|
self.part_batch = pyglet.graphics.Batch()
|
|
|
|
self.label_batch = pyglet.graphics.Batch()
|
2021-08-24 22:31:52 +08:00
|
|
|
# frame
|
2021-10-04 20:41:41 +08:00
|
|
|
self.frame = pyglet.gui.Frame(self, order=20)
|
2021-10-01 23:12:01 +08:00
|
|
|
self.M_frame = pyglet.gui.MovableFrame(self, modifier=key.LCTRL)
|
2022-05-25 09:16:38 +08:00
|
|
|
# self.DRscreen = DRScreen(self)
|
2021-02-03 22:04:34 +08:00
|
|
|
# setup
|
|
|
|
self.setup()
|
2021-10-02 20:40:06 +08:00
|
|
|
# 命令显示
|
2021-10-06 18:36:10 +08:00
|
|
|
self.command_group = pyglet.graphics.Group(0)
|
2022-05-11 11:11:39 +08:00
|
|
|
self.command_tree = tree.CommandTree(tree.DR_command)
|
2021-11-06 19:07:32 +08:00
|
|
|
self.input_box = InputBox(x=50, y=30, width=300, height=20,
|
|
|
|
batch=self.label_batch) # 实例化
|
|
|
|
self.push_handlers(self.input_box)
|
|
|
|
self.input_box.enabled = True
|
2021-10-02 20:40:06 +08:00
|
|
|
# fps显示
|
|
|
|
self.fps_label = pyglet.text.Label(x=10, y=self.height - 10,
|
2021-11-06 19:07:32 +08:00
|
|
|
width=self.width - 20, height=20,
|
2021-10-02 20:40:06 +08:00
|
|
|
anchor_x='left', anchor_y='top',
|
2022-04-30 13:56:57 +08:00
|
|
|
font_name=translate.微软等宽无线, font_size=20,
|
2021-11-06 19:07:32 +08:00
|
|
|
multiline=True,
|
2021-10-06 18:36:10 +08:00
|
|
|
batch=self.label_batch, group=self.command_group)
|
2021-10-02 20:40:06 +08:00
|
|
|
# 设置刷新率
|
2021-09-24 00:04:56 +08:00
|
|
|
pyglet.clock.schedule_interval(self.update, float(self.SPF))
|
2021-10-02 20:40:06 +08:00
|
|
|
# 完成设置后的信息输出
|
2021-10-01 23:12:01 +08:00
|
|
|
self.logger.info(tr.lang('window', 'setup.done'))
|
2021-10-02 20:40:06 +08:00
|
|
|
self.logger.info(tr.lang('window', 'os.pid_is').format(os.getpid(), os.getppid()))
|
2021-10-01 23:12:01 +08:00
|
|
|
end_time = time.time_ns()
|
|
|
|
self.use_time = end_time - start_time
|
|
|
|
self.logger.info(tr.lang('window', 'setup.use_time').format(Decimal(self.use_time) / 1000000000))
|
|
|
|
self.logger.debug(tr.lang('window', 'setup.use_time_ns').format(self.use_time))
|
2021-09-02 22:47:10 +08:00
|
|
|
|
2020-12-16 06:33:33 +08:00
|
|
|
def setup(self):
|
2021-11-06 19:07:32 +08:00
|
|
|
self.load_fonts()
|
2021-10-01 23:12:01 +08:00
|
|
|
|
|
|
|
def load_fonts(self):
|
2021-11-06 19:07:32 +08:00
|
|
|
fonts_folder_path = self.main_config['runtime']['fonts_folder']
|
|
|
|
# 加载字体路径
|
2022-05-11 11:11:39 +08:00
|
|
|
# 淦,还写了个递归来处理
|
|
|
|
pyglet_load_fonts_folder(fonts_folder_path)
|
2021-10-01 23:12:01 +08:00
|
|
|
|
2021-10-25 22:08:00 +08:00
|
|
|
# @new_thread('window load_editor')
|
2022-05-11 11:11:39 +08:00
|
|
|
def load_editor(self):
|
2021-10-01 23:12:01 +08:00
|
|
|
pass
|
2021-02-20 21:46:14 +08:00
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
def start_game(self) -> None:
|
|
|
|
self.run_input = True
|
2022-03-22 23:20:07 +08:00
|
|
|
# self.read_input()
|
2021-09-22 06:21:48 +08:00
|
|
|
pyglet.app.run()
|
|
|
|
|
2021-10-25 22:08:00 +08:00
|
|
|
@new_thread('window read_input', daemon=True)
|
2021-09-22 06:21:48 +08:00
|
|
|
def read_input(self):
|
|
|
|
self.logger.debug('read_input start')
|
|
|
|
while self.run_input:
|
2021-10-23 17:01:59 +08:00
|
|
|
get = input()
|
|
|
|
if get in ('', ' ', '\n', '\r'):
|
|
|
|
continue
|
2021-09-22 06:21:48 +08:00
|
|
|
if get == 'stop':
|
|
|
|
self.run_input = False
|
2021-10-25 22:08:00 +08:00
|
|
|
try:
|
2021-11-06 19:07:32 +08:00
|
|
|
self.on_command(line.CommandText(get))
|
2021-10-31 23:26:32 +08:00
|
|
|
except CommandError:
|
2021-10-25 22:08:00 +08:00
|
|
|
self.logger.error(traceback.format_exc())
|
2021-09-22 06:21:48 +08:00
|
|
|
self.logger.debug('read_input end')
|
|
|
|
|
2021-10-25 22:08:00 +08:00
|
|
|
@new_thread('window save_info')
|
|
|
|
def save_info(self):
|
2022-03-22 23:20:07 +08:00
|
|
|
self.logger.info('save_info start')
|
|
|
|
config_file = tools.load_file('./configs/main.toml')
|
2022-03-22 21:18:04 +08:00
|
|
|
config_file['window']['width'] = self.width
|
|
|
|
config_file['window']['height'] = self.height
|
2022-03-22 23:20:07 +08:00
|
|
|
toml.dump(config_file, open('./configs/main.toml', 'w'))
|
2022-06-04 11:08:30 +08:00
|
|
|
self.logger.info('save_info end')
|
2021-10-25 22:08:00 +08:00
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
"""
|
|
|
|
draws and some event
|
|
|
|
"""
|
2021-01-29 14:07:40 +08:00
|
|
|
|
2021-09-02 22:47:10 +08:00
|
|
|
def update(self, tick: float):
|
2021-10-25 22:08:00 +08:00
|
|
|
decimal_tick = Decimal(str(tick)[:10])
|
2021-10-01 23:12:01 +08:00
|
|
|
self.FPS_update(decimal_tick)
|
2021-10-25 22:08:00 +08:00
|
|
|
self.fps_log.update_tick(Decimal(tick))
|
2021-02-22 21:32:13 +08:00
|
|
|
|
2021-10-01 23:12:01 +08:00
|
|
|
def FPS_update(self, tick: Decimal):
|
2022-06-04 11:08:30 +08:00
|
|
|
now_FPS = pyglet.clock.get_frequency()
|
2021-10-28 06:43:35 +08:00
|
|
|
self.fps_log.update_tick(tick)
|
2022-06-04 11:08:30 +08:00
|
|
|
|
|
|
|
self.fps_label.text = f'FPS: {self.fps_log.fps: >5.1f}({self.fps_log.middle_fps: >5.1f})[{now_FPS: >.7f}]\n {self.fps_log.max_fps: >7.1f} {self.fps_log.min_fps:>5.1f}'
|
2021-02-21 11:58:47 +08:00
|
|
|
|
2021-11-06 19:07:32 +08:00
|
|
|
def on_draw(self, *dt):
|
2022-03-22 23:20:07 +08:00
|
|
|
# self.logger.debug('on_draw call dt: {}'.format(dt))
|
2021-02-22 21:32:13 +08:00
|
|
|
self.clear()
|
2021-02-20 21:46:14 +08:00
|
|
|
self.draw_batch()
|
|
|
|
|
2021-09-02 22:47:10 +08:00
|
|
|
def on_resize(self, width: int, height: int):
|
|
|
|
super().on_resize(width, height)
|
2021-10-02 20:40:06 +08:00
|
|
|
self.fps_label.y = height - 10
|
2021-09-02 22:47:10 +08:00
|
|
|
|
2021-02-20 21:46:14 +08:00
|
|
|
def draw_batch(self):
|
|
|
|
self.part_batch.draw()
|
|
|
|
self.label_batch.draw()
|
2021-02-16 22:28:13 +08:00
|
|
|
|
2021-10-10 16:51:53 +08:00
|
|
|
"""
|
|
|
|
command line event
|
|
|
|
"""
|
|
|
|
|
2021-11-06 19:07:32 +08:00
|
|
|
def on_command(self, command: line.CommandText):
|
2021-10-10 16:51:53 +08:00
|
|
|
self.logger.info(tr.lang('window', 'command.text').format(command))
|
2021-11-06 19:07:32 +08:00
|
|
|
if command.match('stop'):
|
2021-10-23 17:01:59 +08:00
|
|
|
self.dispatch_event('on_close', 'command') # source = command
|
2021-11-06 19:07:32 +08:00
|
|
|
elif command.match('fps'):
|
|
|
|
if command.match('log'):
|
|
|
|
self.logger.debug(self.fps_log.fps_list)
|
|
|
|
elif command.match('max'):
|
|
|
|
self.logger.info(self.fps_log.max_fps)
|
|
|
|
self.command.push_line(self.fps_log.max_fps, block_line=True)
|
|
|
|
elif command.match('min'):
|
|
|
|
self.logger.info(self.fps_log.min_fps)
|
|
|
|
self.command.push_line(self.fps_log.min_fps, block_line=True)
|
|
|
|
elif command.match('default'):
|
|
|
|
self.set_size(int(self.main_config['window_default']['width']), int(self.main_config['window_default']['height']))
|
2022-05-11 11:11:39 +08:00
|
|
|
self.command_tree.parse(command.plain_command)
|
2021-10-10 16:51:53 +08:00
|
|
|
|
2021-10-31 23:26:32 +08:00
|
|
|
def on_message(self, message: line.CommandLine.text):
|
2021-10-10 16:51:53 +08:00
|
|
|
self.logger.info(tr.lang('window', 'message.text').format(message))
|
|
|
|
|
2021-02-16 22:28:13 +08:00
|
|
|
"""
|
2021-01-28 22:50:28 +08:00
|
|
|
keyboard and mouse input
|
2021-02-16 22:28:13 +08:00
|
|
|
"""
|
2021-01-28 22:50:28 +08:00
|
|
|
|
2021-08-13 12:25:29 +08:00
|
|
|
def on_mouse_motion(self, x, y, dx, dy) -> None:
|
2021-04-10 22:48:22 +08:00
|
|
|
pass
|
2020-12-16 06:33:33 +08:00
|
|
|
|
2021-08-13 12:25:29 +08:00
|
|
|
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers) -> None:
|
2021-04-10 22:48:22 +08:00
|
|
|
pass
|
2021-04-09 19:49:02 +08:00
|
|
|
|
2021-08-13 12:25:29 +08:00
|
|
|
def on_mouse_press(self, x, y, button, modifiers) -> None:
|
2021-10-01 23:12:01 +08:00
|
|
|
self.logger.debug(tr.lang('window', 'mouse.press').format([x, y], tr.lang('window', 'mouse.{}'.format(mouse.buttons_string(button)))))
|
2021-08-13 12:25:29 +08:00
|
|
|
|
|
|
|
def on_mouse_release(self, x, y, button, modifiers) -> None:
|
2021-10-01 23:12:01 +08:00
|
|
|
self.logger.debug(tr.lang('window', 'mouse.release').format([x, y], tr.lang('window', 'mouse.{}'.format(mouse.buttons_string(button)))))
|
2021-02-16 22:28:13 +08:00
|
|
|
|
2021-08-13 12:25:29 +08:00
|
|
|
def on_key_press(self, symbol, modifiers) -> None:
|
2021-02-22 23:17:16 +08:00
|
|
|
if symbol == key.ESCAPE and not (modifiers & ~(key.MOD_NUMLOCK |
|
|
|
|
key.MOD_CAPSLOCK |
|
|
|
|
key.MOD_SCROLLLOCK)):
|
|
|
|
self.dispatch_event('on_close')
|
2021-10-02 20:40:06 +08:00
|
|
|
self.logger.debug(tr.lang('window', 'key.press').format(key.symbol_string(symbol), key.modifiers_string(modifiers)))
|
2020-12-16 06:33:33 +08:00
|
|
|
|
2021-08-13 12:25:29 +08:00
|
|
|
def on_key_release(self, symbol, modifiers) -> None:
|
2021-10-02 20:40:06 +08:00
|
|
|
self.logger.debug(tr.lang('window', 'key.release').format(key.symbol_string(symbol), key.modifiers_string(modifiers)))
|
|
|
|
|
|
|
|
def on_text(self, text):
|
2021-10-04 20:41:41 +08:00
|
|
|
if text == '\r':
|
2021-10-02 20:40:06 +08:00
|
|
|
self.logger.debug(tr.lang('window', 'text.new_line'))
|
|
|
|
else:
|
|
|
|
self.logger.debug(tr.lang('window', 'text.input').format(text))
|
2021-11-06 19:07:32 +08:00
|
|
|
if text == 't':
|
|
|
|
self.input_box.enabled = True
|
2021-08-13 12:25:29 +08:00
|
|
|
|
2021-10-04 20:41:41 +08:00
|
|
|
def on_text_motion(self, motion):
|
|
|
|
motion_string = key.motion_string(motion)
|
|
|
|
self.logger.debug(tr.lang('window', 'text.motion').format(motion_string))
|
|
|
|
|
|
|
|
def on_text_motion_select(self, motion):
|
|
|
|
motion_string = key.motion_string(motion)
|
|
|
|
self.logger.debug(tr.lang('window', 'text.motion_select').format(motion_string))
|
|
|
|
|
2021-10-23 17:01:59 +08:00
|
|
|
def on_close(self, source: str = 'window') -> None:
|
|
|
|
self.logger.info(tr.lang('window', 'game.stop_get').format(tr.lang('window', f'game.{source}_stop')))
|
|
|
|
self.logger.info(tr.lang('window', 'game.stop'))
|
2021-10-29 00:15:31 +08:00
|
|
|
self.fps_log.check_list = False
|
2021-10-23 17:01:59 +08:00
|
|
|
if self.run_input:
|
|
|
|
self.run_input = False
|
2021-10-25 22:08:00 +08:00
|
|
|
self.save_info()
|
2021-10-02 20:40:06 +08:00
|
|
|
super().on_close()
|
2021-10-23 17:01:59 +08:00
|
|
|
self.logger.info(tr.lang('window', 'game.end'))
|