body_view.py 2.7 KB
Newer Older
M
march3 已提交
1
# -*- coding:utf-8 -*-
M
march3 已提交
2 3
# title           :天体视图
# description     :天体视图(天体效果展示用)
M
march3 已提交
4
# author          :Python超人
M
march3 已提交
5 6
# date            :2023-02-11
# link            :https://gitcode.net/pythoncr/
M
march3 已提交
7 8 9 10 11 12 13 14 15
# python_version  :3.8
# ==============================================================================
from abc import ABCMeta, abstractmethod
from bodies import Body
from common.func import get_dominant_colors
import numpy as np
import os


M
march3 已提交
16 17 18 19 20
class BodyView(metaclass=ABCMeta):
    """
    天体视图(天体效果展示用)
    """

三月三net's avatar
三月三net 已提交
21
    def __init__(self, body: Body, bodies_system):
M
march3 已提交
22
        self.body = body
三月三net's avatar
三月三net 已提交
23
        self.bodies_system = bodies_system
M
march3 已提交
24 25 26 27 28 29 30 31
        self.sphere = None
        if self.body.texture is None or self.body.texture == '':
            self.color = tuple(np.array(body.color) / 255)
        else:
            self.texture = self.__find_texture(self.body.texture)  # 纹理
            if self.texture is None:
                self.color = tuple(np.array(body.color) / 255)
            else:
M
march3 已提交
32
                self.color = self.__get_texture_main_color(self.texture)
M
march3 已提交
33
        self.appear()
三月三net's avatar
三月三net 已提交
34 35 36 37 38
        self.position = body.position
        self.name = body.name
        self.mass = body.mass
        self.raduis = body.raduis
        self.velocity = body.velocity
M
march3 已提交
39

M
march3 已提交
40 41
        self.appeared = True

M
march3 已提交
42 43 44 45
    def __repr__(self):
        return '<%s> m=%.3e(kg), r=%.3e(km), p=[%.3e,%.3e,%.3e](km), v=%s(km/s)' % \
               (self.name, self.mass, self.raduis,
                self.position[0], self.position[1], self.position[2], self.velocity)
M
march3 已提交
46 47 48

    def __find_texture(self, texture):
        """
M
march3 已提交
49
        尝试在多个路径下寻找纹理图片
M
march3 已提交
50 51 52 53 54 55 56 57 58 59 60
        :param texture: 纹理图片
        :return: 纹理图片的路径
        """
        paths = ['./textures', '../textures']
        for path in paths:
            p = path + "/" + texture
            if os.path.exists(p):
                return p

        return None

M
march3 已提交
61
    def __get_texture_main_color(self, texture):
M
march3 已提交
62
        """
M
march3 已提交
63
        获取纹理图片的主要颜色
M
march3 已提交
64 65 66 67 68 69 70 71 72 73
        :param texture:
        :return:
        """
        colors = get_dominant_colors(texture)
        first_color = colors[0]
        # print(self.name, first_color)
        return tuple(np.array(first_color) / 255)

    @abstractmethod
    def update(self):
M
march3 已提交
74 75 76 77 78 79 80 81 82 83 84
        """
        更新天体信息和数据,比如:更新天体的位置
        :return:
        """
        pass

    def disappear(self):
        """
        天体消失的操作,比如:销毁天体视图对象
        :return:
        """
M
march3 已提交
85 86 87
        pass

    @abstractmethod
M
march3 已提交
88 89 90 91 92
    def appear(self):
        """
        天体显示的操作,比如:构建天体视图对象
        :return:
        """
M
march3 已提交
93
        pass