140 lines
3.8 KiB
Python
140 lines
3.8 KiB
Python
"""
|
|
流体发射器组件
|
|
负责生成流体粒子
|
|
"""
|
|
|
|
from panda3d.core import Vec3, Point3
|
|
import random
|
|
import math
|
|
|
|
class FluidEmitter:
|
|
"""
|
|
流体发射器类
|
|
负责生成流体粒子
|
|
"""
|
|
|
|
def __init__(self, position, fluid_world):
|
|
"""
|
|
初始化流体发射器
|
|
|
|
Args:
|
|
position (Point3): 发射器位置
|
|
fluid_world: 流体世界对象的引用
|
|
"""
|
|
self.position = position
|
|
self.fluid_world = fluid_world
|
|
|
|
# 发射器属性
|
|
self.emit_rate = 10 # 每秒发射粒子数
|
|
self.emit_velocity = Vec3(0, 0, 5) # 发射速度
|
|
self.emit_spread = 0.1 # 发射扩散角度
|
|
self.emit_count = 0 # 已发射粒子数
|
|
self.is_active = True # 是否激活
|
|
|
|
# 粒子属性
|
|
self.particle_mass = 1.0 # 粒子质量
|
|
self.particle_lifespan = 5.0 # 粒子寿命
|
|
|
|
print(f"流体发射器创建完成: {position}")
|
|
|
|
def update(self, dt):
|
|
"""
|
|
更新发射器状态
|
|
|
|
Args:
|
|
dt (float): 时间步长
|
|
"""
|
|
if not self.is_active:
|
|
return
|
|
|
|
# 计算应该发射的粒子数
|
|
particles_to_emit = int(self.emit_rate * dt)
|
|
|
|
# 发射粒子
|
|
for _ in range(particles_to_emit):
|
|
self._emit_particle()
|
|
|
|
def _emit_particle(self):
|
|
"""
|
|
发射单个粒子
|
|
"""
|
|
# 创建粒子对象(这里简化为字典,实际实现中应该是专门的粒子类)
|
|
particle = {
|
|
'position': self.position + Vec3(
|
|
random.uniform(-0.1, 0.1),
|
|
random.uniform(-0.1, 0.1),
|
|
random.uniform(-0.1, 0.1)
|
|
),
|
|
'velocity': self._get_random_velocity(),
|
|
'mass': self.particle_mass,
|
|
'lifespan': self.particle_lifespan,
|
|
'age': 0.0
|
|
}
|
|
|
|
# 添加到流体世界
|
|
self.fluid_world.add_fluid_particle(particle)
|
|
self.emit_count += 1
|
|
|
|
def _get_random_velocity(self):
|
|
"""
|
|
获取随机发射速度
|
|
|
|
Returns:
|
|
Vec3: 随机速度向量
|
|
"""
|
|
# 基础发射方向
|
|
base_direction = self.emit_velocity.normalized()
|
|
|
|
# 添加随机扩散
|
|
angle = random.uniform(0, 2 * math.pi)
|
|
spread = random.uniform(0, self.emit_spread)
|
|
|
|
# 简化的扩散计算
|
|
spread_x = math.cos(angle) * spread
|
|
spread_y = math.sin(angle) * spread
|
|
|
|
# 应用扩散到速度向量
|
|
velocity = base_direction + Vec3(spread_x, spread_y, 0)
|
|
velocity.normalize()
|
|
velocity *= self.emit_velocity.length()
|
|
|
|
return velocity
|
|
|
|
def set_emit_rate(self, rate):
|
|
"""
|
|
设置发射速率
|
|
|
|
Args:
|
|
rate (float): 每秒发射粒子数
|
|
"""
|
|
self.emit_rate = rate
|
|
|
|
def set_emit_velocity(self, velocity):
|
|
"""
|
|
设置发射速度
|
|
|
|
Args:
|
|
velocity (Vec3): 发射速度向量
|
|
"""
|
|
self.emit_velocity = velocity
|
|
|
|
def set_emit_spread(self, spread):
|
|
"""
|
|
设置发射扩散角度
|
|
|
|
Args:
|
|
spread (float): 扩散角度(弧度)
|
|
"""
|
|
self.emit_spread = spread
|
|
|
|
def activate(self):
|
|
"""
|
|
激活发射器
|
|
"""
|
|
self.is_active = True
|
|
|
|
def deactivate(self):
|
|
"""
|
|
停用发射器
|
|
"""
|
|
self.is_active = False |