ursina_view.py 2.3 KB
Newer Older
三月三net's avatar
三月三net 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# -*- coding:utf-8 -*-
# title           :ursina天体视图
# description     :ursina天体视图(天体效果展示用,需要安装 ursina)
# author          :Python超人
# date            :2023-02-11
# link            :https://gitcode.net/pythoncr/
# python_version  :3.8
# ==============================================================================
# pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com ursina
from ursina import Ursina, window, Entity, camera, color, mouse, Vec2, load_texture, held_keys
from bodies import Body
import random
from simulators.views.body_view import BodyView
import numpy as np
from math import sin, cos, radians
import os


class Planet(Entity):
    def __init__(self, name, texture, pos, scale=2):
        self.angle = random.uniform(0.0005, 0.01)
        self.fastMode = 0
        self.rotation = (random.randint(0, 360) for i in range(3))
        self.rotspeed = random.uniform(0.25, 1.5)
        self.rotMode = random.choice(["x", "y", "z"])
        self.name = name
        # texture = eval(f"{_type}_texture")
        # e = os.path.exists(texture)
        texture = load_texture(texture)
        super().__init__(model="sphere",
                         scale=scale,
                         texture=texture,
                         color=color.white,
                         position=pos)

    def turn(self, angle):
M
march3 已提交
37 38 39 40 41 42
        # if self.name != "sun":
        #     if self.fastMode:
        #         angle *= 200
        # self.x = self.x * cos(radians(angle)) - self.y * sin(radians(angle))
        # self.y = self.x * sin(radians(angle)) + self.y * cos(radians(angle))
        exec(f"self.rotation_{self.rotMode}+=self.rotspeed")
三月三net's avatar
三月三net 已提交
43 44 45 46 47 48 49 50 51 52 53

    #
    def input(self, key):
        if key == "enter":
            self.fastMode = 1 - self.fastMode


class UrsinaView(BodyView):
    """
    ursina天体视图(天体效果展示用)
    """
M
march3 已提交
54
    SCALE = 1e-6
三月三net's avatar
三月三net 已提交
55 56 57

    def __init__(self, body: Body):
        BodyView.__init__(self, body)
M
march3 已提交
58 59 60
        pos = self.position * UrsinaView.SCALE
        size = body.diameter * body.size_scale * UrsinaView.SCALE
        self.entity = Planet(body.name, self.texture, pos, size)
三月三net's avatar
三月三net 已提交
61 62

    def update(self):
M
march3 已提交
63
        self.entity.turn(self.entity.angle)
三月三net's avatar
三月三net 已提交
64 65 66 67 68

    def appear(self):
        pass

    def disappear(self):
M
march3 已提交
69
        self.entity.disable()