Difficult-Rocket/bin/client.py

277 lines
10 KiB
Python
Raw Normal View History

2021-02-20 21:46:14 +08:00
"""
2020-12-13 18:39:12 +08:00
writen by shenjackyuanjie
mail: 3695888@qq.com
2021-02-20 21:46:14 +08:00
"""
2020-12-13 18:39:12 +08:00
2021-04-17 01:14:38 +08:00
import multiprocessing as mp
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
2021-04-03 12:15:24 +08:00
2021-04-02 23:31:54 +08:00
sys.path.append('./bin/libs/')
sys.path.append('./')
import pyglet
2021-02-22 23:17:16 +08:00
from pyglet.window import key
from pyglet.window import mouse
2020-12-13 18:39:12 +08:00
2021-04-02 23:31:54 +08:00
try:
from bin import tools
from bin import configs
except (ModuleNotFoundError, ImportError, ImportWarning):
import tools
import configs
2021-02-03 22:04:34 +08:00
2020-12-16 06:33:33 +08:00
class client(mp.Process):
2021-02-21 11:58:47 +08:00
def __init__(self, logger, dev_dic=None, dev_list=None, language='zh-cn', net_mode='local'):
mp.Process.__init__(self)
# logging
self.logger = logger
# share memory
self.dev_list = dev_list
self.dev_dic = dev_dic
2021-02-22 23:17:16 +08:00
# lang
2021-04-02 23:31:54 +08:00
self.lang = tools.config('configs/sys_value/lang/%s.json5' % language, 'client')
# value
self.process_id = 'Client'
self.process_name = 'Client process'
2021-02-20 21:46:14 +08:00
self.process_pid = os.getpid()
self.view = 'space'
self.net_mode = net_mode
2021-04-02 23:31:54 +08:00
self.window_config = tools.config('configs/sys_value/window.json5')
2021-02-22 23:27:18 +08:00
self.caption = self.window_config['caption']
self.caption = configs.name_handler(self.caption, {'version': self.window_config['caption_option']['version']})
self.window = window(logger=logger,
dev_dic=dev_dic,
dev_list=dev_list,
2021-02-21 11:58:47 +08:00
language=language,
net_mode=net_mode,
width=int(self.window_config['width']),
height=int(self.window_config['height']),
fullscreen=tools.c_b(self.window_config['full_screen']),
2021-02-22 23:27:18 +08:00
caption=self.caption,
resizable=tools.c_b(self.window_config['resizable']),
visible=tools.c_b(self.window_config['visible']))
2021-02-20 21:46:14 +08:00
self.log_config()
2021-02-20 21:46:14 +08:00
def log_config(self):
2021-04-02 23:31:54 +08:00
self.logger.info('%s: %s %s' % (self.lang['os.pid_is1'], self.process_pid, self.lang['os.pid_is2']))
2021-02-20 21:46:14 +08:00
def run(self) -> None:
pyglet.app.run()
2021-01-11 21:58:51 +08:00
class window(pyglet.window.Window):
2021-02-21 11:58:47 +08:00
def __init__(self, logger, dev_dic=None, dev_list=None, language='zh-cn', net_mode='local', *args, **kwargs):
super(window, self).__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
self.logger = logger
# share memory
2021-01-21 15:05:29 +08:00
self.dev_list = dev_list
self.dev_dic = dev_dic
2021-02-03 22:04:34 +08:00
# value
self.FPS = int(tools.config('configs/sys_value/window.json5')['fps'])
2021-02-21 11:58:47 +08:00
self.SPF = 1.0 / self.FPS
2021-02-03 22:04:34 +08:00
self.view = 'space'
self.net_mode = net_mode
2021-02-22 12:28:13 +08:00
# FPS
2021-02-22 21:32:13 +08:00
self.max_fps = [self.FPS, time.time()]
self.min_fps = [self.FPS, time.time()]
self.fps_wait = 5
# lang
2021-04-02 23:31:54 +08:00
self.lang = tools.config('configs/sys_value/lang/%s.json5' % language, 'client')
2021-02-03 22:04:34 +08:00
# configs
2021-02-13 22:41:44 +08:00
self.view = tools.config('configs/view.json5')
2021-02-06 16:41:54 +08:00
self.map_view = [configs.basic_poi(poi_type='chunk')]
2021-04-02 23:31:54 +08:00
self.part_list = tools.config('configs/sys_value/parts.json5')
2021-02-22 23:17:16 +08:00
pyglet.resource.path = ['textures']
pyglet.resource.reindex()
2020-12-16 06:33:33 +08:00
# dic
2021-02-25 18:45:02 +08:00
self.button_hitbox = {}
2021-02-26 13:59:37 +08:00
self.button_toggled = {}
2021-02-07 13:50:19 +08:00
self.ships = {} # all ship(part)
2021-04-02 23:31:54 +08:00
self.planet_system = tools.config('configs/sys_value/planet.json5') # hole planet system
2020-12-16 06:33:33 +08:00
# list
2021-02-20 21:46:14 +08:00
# batch
self.part_batch = pyglet.graphics.Batch()
self.label_batch = pyglet.graphics.Batch()
self.runtime_batch = pyglet.graphics.Batch()
2021-02-03 22:04:34 +08:00
# window
2021-02-22 21:32:13 +08:00
self.logger.info('%s' % self.lang['setup.done'])
self.textures = {}
2021-02-03 22:04:34 +08:00
# setup
self.setup()
2021-02-22 12:28:13 +08:00
pyglet.clock.schedule_interval(self.update, self.SPF)
2020-12-16 06:33:33 +08:00
def setup(self):
2021-02-25 18:45:02 +08:00
# values
2021-02-03 22:04:34 +08:00
# net_mode
if self.net_mode == 'local':
pass
2021-04-02 23:31:54 +08:00
2021-02-22 23:17:16 +08:00
# parts textures
2021-02-13 22:41:44 +08:00
self.textures['part'] = {}
2021-04-02 23:31:54 +08:00
parts = tools.config('configs/sys_value/parts.json5')
2021-02-13 22:41:44 +08:00
for part in parts:
path = parts[part][2][0]
2021-02-22 23:17:16 +08:00
part_image = pyglet.resource.image(path)
self.textures['part'][part] = part_image
2021-04-02 23:31:54 +08:00
2021-02-22 23:17:16 +08:00
# runtimes textures
self.textures['runtime'] = {}
2021-04-02 23:31:54 +08:00
runtimes = tools.config('configs/sys_value/runtime.json5')
2021-02-26 13:42:22 +08:00
# load textures
2021-02-25 18:45:02 +08:00
for runtime in runtimes['textures']:
path = runtimes['textures'][runtime]
2021-02-22 23:17:16 +08:00
runtime_image = pyglet.resource.image(path)
self.textures['runtime'][runtime] = runtime_image
2021-02-26 13:42:22 +08:00
# load button's textures
2021-02-25 18:45:02 +08:00
for runtime in runtimes['button']:
2021-02-27 20:55:08 +08:00
if runtime == 'logic':
continue
2021-02-25 18:45:02 +08:00
path = runtimes['button'][runtime]
runtime_image = pyglet.resource.image(path)
runtime_sprite = pyglet.sprite.Sprite(img=runtime_image, batch=self.runtime_batch, x=self.width + 1,
y=self.height + 1)
2021-02-26 13:42:22 +08:00
# self.textures['runtime'][runtime] = runtime_image
self.textures['runtime'][runtime] = runtime_sprite
self.button_hitbox[runtime] = [runtime_image.width, runtime_image.height]
2021-02-26 13:59:37 +08:00
self.button_toggled[runtime] = -1
2021-02-25 18:45:02 +08:00
# info_label
2021-02-21 11:58:47 +08:00
self.info_label = pyglet.text.Label(text='test %s' % pyglet.clock.get_fps(),
x=10, y=self.height - 10,
anchor_x='left', anchor_y='top',
2021-02-20 21:46:14 +08:00
batch=self.label_batch)
# draws
2021-01-29 14:07:40 +08:00
2021-02-22 12:28:13 +08:00
def update(self, ree):
2021-02-22 21:32:13 +08:00
self.FPS_update()
2021-02-26 13:42:22 +08:00
self.hit_box_update()
2021-02-22 21:32:13 +08:00
def FPS_update(self):
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.info_label.text = 'now FPS: %03d max FPS: %02d min FPS: %02d' % (
now_FPS, self.max_fps[0], self.min_fps[0])
2021-02-26 19:05:24 +08:00
self.info_label.anchor_x = 'left'
self.info_label.anchor_y = 'top'
self.info_label.x = 10
self.info_label.y = self.height - 10
2021-02-21 11:58:47 +08:00
2021-02-26 13:42:22 +08:00
def hit_box_update(self):
for hit_box in self.button_hitbox:
box_ = self.button_hitbox[hit_box]
button = self.textures['runtime'][hit_box]
box = [button.x, button.y, button.x + button.width, button.y + button.height]
self.button_hitbox[hit_box] = box
2020-12-16 06:33:33 +08:00
def on_draw(self):
2021-02-22 21:32:13 +08:00
self.clear()
self.build_draw()
2021-02-20 21:46:14 +08:00
self.draw_batch()
def draw_batch(self):
self.part_batch.draw()
self.runtime_batch.draw()
self.label_batch.draw()
2021-01-29 14:07:40 +08:00
2021-02-14 10:51:13 +08:00
def build_draw(self):
2021-02-22 23:17:16 +08:00
self.textures['runtime']['trash_can'].blit(x=self.width - 90, y=self.height - 90)
2021-02-25 18:45:02 +08:00
self.textures['runtime']['trash_can'].blit(x=self.width - 90, y=self.height - 90)
# button tool bar
# start from 20 20
# between 30
# size 50*50
tool_y = 25
back = 0
while back < self.width:
self.textures['runtime']['toolbar_light'].blit(x=back, y=0)
back += self.textures['runtime']['toolbar_light'].width - 1
2021-02-26 13:42:22 +08:00
self.textures['runtime']['to_menu'].x = 20
self.textures['runtime']['to_menu'].y = tool_y
self.textures['runtime']['add_part'].x = 100
self.textures['runtime']['add_part'].y = tool_y
self.textures['runtime']['stage'].x = 180
self.textures['runtime']['stage'].y = tool_y
self.textures['runtime']['zoom'].x = 260
self.textures['runtime']['zoom'].y = tool_y
2021-03-24 23:37:10 +08:00
self.textures['runtime']['play'].x = self.width - 50 - 20
self.textures['runtime']['play'].y = tool_y
2021-02-26 13:59:37 +08:00
if self.button_toggled['zoom'] != -1:
2021-02-26 13:42:22 +08:00
self.textures['runtime']['zoom_in'].x = 260 - 40
self.textures['runtime']['zoom_in'].y = tool_y + 25 + 50
self.textures['runtime']['zoom_out'].x = 260 + 40
self.textures['runtime']['zoom_out'].y = tool_y + 25 + 50
2021-02-25 18:45:02 +08:00
else:
2021-02-26 19:05:24 +08:00
self.button_toggled['zoom_in'] = -1
self.button_toggled['zoom_out'] = -1
2021-02-26 13:42:22 +08:00
self.textures['runtime']['zoom_in'].x = self.width + 1
self.textures['runtime']['zoom_out'].x = self.width + 1
2021-02-25 18:45:02 +08:00
2021-02-22 23:42:30 +08:00
# //todo 把所有要素都加进来+整个设置图标+布局
2021-02-14 10:51:13 +08:00
def space_draw(self):
2021-01-28 19:43:26 +08:00
# render parts
2021-02-26 13:42:22 +08:00
2021-02-02 16:45:20 +08:00
for ship in self.ships:
2021-02-03 22:04:34 +08:00
# get ship poi
2021-01-28 22:50:28 +08:00
ship_poi = ship['brain'][3]
2021-02-03 22:04:34 +08:00
distances = tools.distance(ship_poi, self.map_view)
2021-01-28 22:07:57 +08:00
for part in ship:
pass
2020-12-16 06:33:33 +08:00
def draw_label(self):
pass
"""
2021-01-28 22:50:28 +08:00
keyboard and mouse input
"""
2021-01-28 22:50:28 +08:00
def on_mouse_motion(self, x, y, dx, dy):
2021-04-10 22:48:22 +08:00
# self.logger.debug('按键移动 %s %s %s %s' % (x, y, dx, dy))
pass
2020-12-16 06:33:33 +08:00
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
2021-04-10 22:48:22 +08:00
# self.logger.debug('按键拖拽 %s %s %s %s %s %s' %(x, y, dx, dy, buttons, modifiers))
pass
def on_mouse_press(self, x, y, button, modifiers):
2021-02-22 23:17:16 +08:00
if button == mouse.LEFT:
2021-02-26 19:05:24 +08:00
self.logger.debug('左键! 在 x:%s y:%s' % (x, y))
2021-02-26 13:42:22 +08:00
for hit_box in self.button_hitbox:
box = self.button_hitbox[hit_box]
if (box[0] <= x <= box[2]) and (box[1] <= y <= box[3]):
2021-02-26 13:59:37 +08:00
self.button_toggled[hit_box] *= -1
2021-02-26 19:05:24 +08:00
self.logger.debug('%s %s %s' % (hit_box,
self.lang['button.been_press'],
self.button_toggled[hit_box]))
2021-02-26 13:42:22 +08:00
break
2021-02-22 23:17:16 +08:00
elif button == mouse.RIGHT:
2021-02-26 19:05:24 +08:00
self.logger.debug('右键! 在 x:%s y:%s' % (x, y))
def on_key_press(self, symbol, modifiers):
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')
2020-12-16 06:33:33 +08:00
def on_key_release(self, symbol, modifiers):
2020-12-16 06:33:33 +08:00
pass