Difficult-Rocket/Difficult_Rocket/client.py

273 lines
10 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2021 by shenjackyuanjie
# 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
# 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
import logging
import configparser
2021-09-24 00:04:56 +08:00
from decimal import Decimal
2021-08-24 22:31:52 +08:00
if __name__ == '__main__': # been start will not run this
sys.path.append('/bin/libs')
sys.path.append('/bin')
2021-07-08 20:42:22 +08:00
# Difficult_Rocket function
2021-10-23 17:01:59 +08:00
from . import guis
from .api import command
from .api.Exp import *
from .api.translate import tr
from .api import tools, new_thread, translate
2021-02-03 22:04:34 +08:00
# libs function
local_lib = True
if local_lib:
from libs import pyglet
from libs.pyglet.window import key, mouse
else:
import pyglet
from pyglet.window import key, mouse
2020-12-16 06:33:33 +08:00
2021-08-13 12:25:29 +08:00
class Client:
def __init__(self, net_mode='local'):
2021-10-01 23:12:01 +08:00
start_time = time.time_ns()
# logging
2021-08-13 12:25:29 +08:00
self.logger = logging.getLogger('client')
# config
self.config = tools.load_file('configs/main.config')
# value
self.process_id = 'Client'
self.process_name = 'Client process'
2021-02-20 21:46:14 +08:00
self.process_pid = os.getpid()
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']})
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
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-08-13 12:25:29 +08:00
class ClientWindow(pyglet.window.Window):
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
self.run_input = False
2021-08-24 22:31:52 +08:00
# configs
pyglet.resource.path = ['textures']
pyglet.resource.reindex()
self.config_file = tools.load_file('configs/main.config')
self.game_config = tools.load_file('configs/game.config')
2021-09-24 00:04:56 +08:00
self.FPS = Decimal(int(self.config_file['runtime']['fps']))
self.SPF = Decimal('1') / self.FPS
2020-12-16 06:33:33 +08:00
# dic
2021-09-02 22:47:10 +08:00
self.environment = {}
self.textures = {} # all textures
self.runtime = {}
# FPS
self.max_fps = [self.FPS, time.time()]
self.min_fps = [self.FPS, time.time()]
self.fps_wait = 5
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
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)
2021-02-03 22:04:34 +08:00
# setup
self.setup()
# 命令显示
self.command_group = pyglet.graphics.Group(0)
2021-10-23 17:01:59 +08:00
self.command = command.CommandLine(x=50, y=30, # 实例化
width=self.width - 100, height=40,
length=int(self.game_config['command']['show']),
batch=self.label_batch, group=self.command_group)
self.push_handlers(self.command)
self.command.set_handler('on_command', self.on_command)
self.command.set_handler('on_message', self.on_message)
# fps显示
self.fps_label = pyglet.text.Label(x=10, y=self.height - 10,
anchor_x='left', anchor_y='top',
font_name=translate.鸿蒙简体, font_size=20,
batch=self.label_batch, group=self.command_group)
# 设置刷新率
2021-09-24 00:04:56 +08:00
pyglet.clock.schedule_interval(self.update, float(self.SPF))
# 完成设置后的信息输出
2021-10-01 23:12:01 +08:00
self.logger.info(tr.lang('window', 'setup.done'))
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-10-23 17:01:59 +08:00
self.load_fonts().join()
# print(pyglet.font.load(translate.鸿蒙简体))
2021-10-01 23:12:01 +08:00
2021-10-23 17:01:59 +08:00
@new_thread('client load_fonts')
2021-10-01 23:12:01 +08:00
def load_fonts(self):
2021-10-23 17:01:59 +08:00
file_path = './libs/fonts/HarmonyOS_Sans/'
2021-10-01 23:12:01 +08:00
ttf_files = os.listdir(file_path)
self.logger.info(tr.lang('window', 'fonts.found').format(ttf_files))
2021-10-23 17:01:59 +08:00
for ttf_folder in ttf_files:
for ttf_file in os.listdir(f'{file_path}{ttf_folder}'):
if not ttf_file[-4:] == '.ttf': continue
pyglet.font.add_file(f'{file_path}{ttf_folder}/{ttf_file}')
# print(f'{file_path}{ttf_file}')
2021-10-01 23:12:01 +08:00
2021-10-23 17:01:59 +08:00
# @new_thread('client load_editor')
2021-10-01 23:12:01 +08:00
def load_Editor(self):
pass
2021-02-20 21:46:14 +08:00
def start_game(self) -> None:
self.run_input = True
2021-10-23 17:01:59 +08:00
self.read_input()
pyglet.app.run()
2021-10-23 17:01:59 +08:00
@new_thread('read_input', daemon=True)
def read_input(self):
self.logger.debug('read_input start')
while self.run_input:
2021-10-23 17:01:59 +08:00
self.logger.info('<<<')
get = input()
if get in ('', ' ', '\n', '\r'):
continue
self.logger.info(get)
if get == 'stop':
self.run_input = False
2021-10-23 17:01:59 +08:00
self.dispatch_event('on_close', 'a') # source = 'command'
self.logger.debug('read_input end')
"""
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-01 23:12:01 +08:00
decimal_tick = Decimal(tick)
self.FPS_update(decimal_tick)
2021-02-22 21:32:13 +08:00
2021-10-01 23:12:01 +08:00
def FPS_update(self, tick: Decimal):
2021-02-22 12:28:13 +08:00
now_FPS = pyglet.clock.get_fps()
2021-02-22 21:32:13 +08:00
if now_FPS > self.max_fps[0]:
self.max_fps = [now_FPS, time.time()]
elif now_FPS < self.min_fps[0]:
self.min_fps = [now_FPS, time.time()]
else:
if (time.time() - self.max_fps[1]) > self.fps_wait:
self.max_fps = [self.FPS, time.time()]
elif (time.time() - self.min_fps[1]) > self.fps_wait:
self.min_fps = [self.FPS, time.time()]
self.fps_label.text = 'FPS: {:.1f} {:.1f} ({:.1f}/{:.1f}) | MSPF: {:.5f} '.format(now_FPS, 1 / tick, self.max_fps[0], self.min_fps[0], tick)
2021-02-21 11:58:47 +08:00
2020-12-16 06:33:33 +08:00
def on_draw(self):
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)
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()
"""
command line event
"""
2021-10-23 17:01:59 +08:00
def on_command(self, command: command.CommandLine.text):
self.logger.info(tr.lang('window', 'command.text').format(command))
2021-10-23 17:01:59 +08:00
if command == 'stop':
self.dispatch_event('on_close', 'command') # source = command
2021-10-23 17:01:59 +08:00
def on_message(self, message: command.CommandLine.text):
self.logger.info(tr.lang('window', 'message.text').format(message))
"""
2021-01-28 22:50:28 +08:00
keyboard and mouse input
"""
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-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-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')
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:
self.logger.debug(tr.lang('window', 'key.release').format(key.symbol_string(symbol), key.modifiers_string(modifiers)))
def on_text(self, text):
if text == '\r':
self.logger.debug(tr.lang('window', 'text.new_line'))
else:
self.logger.debug(tr.lang('window', 'text.input').format(text))
2021-08-13 12:25:29 +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'))
if self.run_input:
self.run_input = False
2021-08-13 12:25:29 +08:00
config_file = configparser.ConfigParser()
config_file.read('configs/main.config')
config_file['window']['width'] = str(self.width)
config_file['window']['height'] = str(self.height)
config_file.write(open('configs/main.config', 'w', encoding='utf-8'))
super().on_close()
2021-10-23 17:01:59 +08:00
self.logger.info(tr.lang('window', 'game.end'))