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-02-20 21:46:14 +08:00
|
|
|
import os
|
2021-02-21 11:58:47 +08:00
|
|
|
import time
|
2021-01-23 21:43:04 +08:00
|
|
|
import pyglet
|
2021-02-21 11:58:47 +08:00
|
|
|
import random
|
2021-02-13 22:41:44 +08:00
|
|
|
from pyglet import image
|
2021-02-20 21:46:14 +08:00
|
|
|
import multiprocessing as mp
|
2020-12-13 18:39:12 +08:00
|
|
|
|
2021-02-03 22:04:34 +08:00
|
|
|
try:
|
2021-02-13 22:41:44 +08:00
|
|
|
# been import use
|
2021-02-03 22:04:34 +08:00
|
|
|
from bin import configs
|
|
|
|
from bin import tools
|
2021-02-14 11:07:02 +08:00
|
|
|
except (ModuleNotFoundError, ImportError, ImportWarning):
|
2021-02-14 00:19:52 +08:00
|
|
|
# editing use
|
|
|
|
import configs
|
|
|
|
import tools
|
2021-02-03 22:04:34 +08:00
|
|
|
|
2020-12-16 06:33:33 +08:00
|
|
|
|
2021-02-16 22:28:13 +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'):
|
2021-02-16 22:28:13 +08:00
|
|
|
mp.Process.__init__(self)
|
|
|
|
# logging
|
|
|
|
self.logger = logger
|
|
|
|
# share memory
|
|
|
|
self.dev_list = dev_list
|
|
|
|
self.dev_dic = dev_dic
|
|
|
|
# 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.view = 'space'
|
|
|
|
self.net_mode = net_mode
|
|
|
|
self.window_config = tools.config('sys_value/window.json5')
|
|
|
|
self.window = window(logger=logger,
|
|
|
|
dev_dic=dev_dic,
|
|
|
|
dev_list=dev_list,
|
2021-02-21 11:58:47 +08:00
|
|
|
language=language,
|
2021-02-16 22:28:13 +08:00
|
|
|
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']),
|
|
|
|
caption=str(self.window_config['caption']),
|
|
|
|
visible=tools.c_b(self.window_config['visible']))
|
2021-02-20 21:46:14 +08:00
|
|
|
self.log_config()
|
2021-02-16 22:28:13 +08:00
|
|
|
|
2021-02-20 21:46:14 +08:00
|
|
|
def log_config(self):
|
|
|
|
self.logger.info('client is running on pid : %s' % self.process_pid)
|
|
|
|
|
|
|
|
def run(self) -> None:
|
2021-02-16 22:28:13 +08:00
|
|
|
pyglet.app.run()
|
2021-01-11 21:58:51 +08:00
|
|
|
|
2021-02-16 22:28:13 +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):
|
2021-02-16 22:28:13 +08:00
|
|
|
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
|
2021-01-24 22:00:31 +08:00
|
|
|
# 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
|
2021-02-21 11:58:47 +08:00
|
|
|
self.FPS = 60
|
|
|
|
self.SPF = 1.0 / self.FPS
|
2021-02-03 22:04:34 +08:00
|
|
|
self.view = 'space'
|
|
|
|
self.net_mode = net_mode
|
|
|
|
# configs
|
2021-02-21 11:58:47 +08:00
|
|
|
self.lang = tools.config('sys_value/lang/%s.json5' % language)
|
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-02-13 22:41:44 +08:00
|
|
|
self.part_list = tools.config('sys_value/parts.json5')
|
2020-12-16 06:33:33 +08:00
|
|
|
# dic
|
2021-02-07 13:50:19 +08:00
|
|
|
self.ships = {} # all ship(part)
|
2021-02-20 21:46:14 +08:00
|
|
|
self.planet_system = tools.config('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-15 21:45:20 +08:00
|
|
|
self.logger.info('client setup done!')
|
2021-02-16 22:28:13 +08:00
|
|
|
self.textures = {}
|
2021-02-03 22:04:34 +08:00
|
|
|
# setup
|
|
|
|
self.setup()
|
2021-02-21 11:58:47 +08:00
|
|
|
pyglet.clock.schedule_interval(self.update(), self.SPF)
|
2020-12-16 06:33:33 +08:00
|
|
|
|
|
|
|
def setup(self):
|
2021-02-03 22:04:34 +08:00
|
|
|
# net_mode
|
|
|
|
if self.net_mode == 'local':
|
|
|
|
pass
|
2021-02-13 22:41:44 +08:00
|
|
|
# parts
|
|
|
|
self.textures['part'] = {}
|
2021-02-14 22:15:05 +08:00
|
|
|
parts = tools.config('sys_value/parts.json5')
|
2021-02-13 22:41:44 +08:00
|
|
|
for part in parts:
|
2021-02-14 22:15:05 +08:00
|
|
|
path = parts[part][2][0]
|
2021-02-20 21:46:14 +08:00
|
|
|
part_image = image.load('textures/' + path)
|
2021-02-14 22:15:05 +08:00
|
|
|
self.textures['part'][part] = part_image
|
|
|
|
|
2021-02-20 21:46:14 +08:00
|
|
|
# tests
|
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)
|
|
|
|
|
2021-02-14 22:15:05 +08:00
|
|
|
# draws
|
2021-01-29 14:07:40 +08:00
|
|
|
|
2021-02-21 11:58:47 +08:00
|
|
|
def update(self):
|
|
|
|
self.info_label.x = random.randint(100, 500)
|
|
|
|
self.info_label.y = random.randint(100, 500)
|
|
|
|
|
2020-12-16 06:33:33 +08:00
|
|
|
def on_draw(self):
|
2021-02-20 21:46:14 +08:00
|
|
|
self.draw_batch()
|
2021-02-21 11:58:47 +08:00
|
|
|
print(time.time())
|
2021-02-20 21:46:14 +08:00
|
|
|
|
|
|
|
def draw_batch(self):
|
2021-02-21 11:58:47 +08:00
|
|
|
self.clear()
|
2021-02-20 21:46:14 +08:00
|
|
|
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):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def space_draw(self):
|
2021-01-28 19:43:26 +08:00
|
|
|
# render parts
|
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
|
|
|
|
2021-02-16 22:28:13 +08:00
|
|
|
def draw_label(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
"""
|
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-01-24 22:00:31 +08:00
|
|
|
def on_mouse_motion(self, x, y, dx, dy):
|
2020-12-16 06:33:33 +08:00
|
|
|
pass
|
|
|
|
|
2021-02-16 22:28:13 +08:00
|
|
|
def on_mouse_press(self, x, y, button, modifiers):
|
|
|
|
pass
|
|
|
|
|
2021-01-24 22:00:31 +08:00
|
|
|
def on_key_press(self, symbol, modifiers):
|
2020-12-16 06:33:33 +08:00
|
|
|
pass
|
|
|
|
|
2021-01-24 22:00:31 +08:00
|
|
|
def on_key_release(self, symbol, modifiers):
|
2020-12-16 06:33:33 +08:00
|
|
|
pass
|