ursina_view.py 2.2 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
# -*- 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):
        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")

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


class UrsinaView(BodyView):
    """
    ursina天体视图(天体效果展示用)
    """

    def __init__(self, body: Body):
        BodyView.__init__(self, body)
        self.entity = Planet(body.name, self.texture, tuple(self.position/1e4), body.size_scale)

    def update(self):
        self.entity.turn(10)

    def appear(self):
        pass

    def disappear(self):
        pass