pyxen.Camera
Camera 定义了渲染器在世界中看到的内容。
它作为组件附加到实体上。渲染器使用摄像机的变换和属性来计算视图和投影矩阵。
世界中必须至少包含一个摄像机才能渲染任何内容。
创建摄像机
camera = world.spawn(
x=0,
y=0,
camera=Camera()
)
或稍后添加:
camera = world.spawn()
camera.camera = Camera()
摄像机使用实体变换
摄像机不存储自己的位置——它使用实体的变换:
camera.x = 100
camera.y = 50
camera.rotation = 30
camera.scale = (2, 2)
移动实体就移动了视图。父节点变换也会影响摄像机。
属性
zoom
camera.camera.zoom = 1.0
类型: float
默认值: 1.0
| 值 | 效果 |
|---|---|
1.0 | 默认视图 |
>1 | 放大 |
<1 | 缩小 |
示例:
camera.camera.zoom = 2.0 # zoom in
camera.camera.zoom = 0.5 # zoom out
缩放仅影响投影矩阵,不影响实体变换。
示例:跟随玩家
player = world.spawn(x=0, y=0, sprite=hero)
camera = world.spawn(camera=Camera())
def update():
camera.x = player.x
camera.y = player.y
或更简单,将摄像机的父节点设为玩家:
player = world.spawn(x=0, y=0, sprite=hero)
camera = world.spawn(camera=Camera(), parent=player)
缩小的摄像机:
camera.camera.zoom = 0.75
屏幕震动与闪光
摄像机可以应用两种屏幕效果,二者均以帧为单位(Pyxen 以固定的 30 fps 运行):
cam = entity.camera
cam.shake(intensity, duration=8) # jolt the view; fades out over `duration`
cam.flash(color, duration=8) # full-screen colour that fades out
| 方法 | 参数 | 描述 |
|---|---|---|
shake(intensity, duration=8) | intensity 以像素为单位,duration 以帧为单位 | 以确定性的、逐渐衰减的偏移量抖动视图。 |
flash(color, duration=8) | color 为 Color 或 (r, g, b[, a]),duration 以帧为单位 | 让全屏颜色叠加在 duration 帧内淡出。 |
def on_hit():
entity.camera.shake(3.0, 10)
entity.camera.flash((1.0, 0.0, 0.0, 0.6), 6)
两种效果都是确定性的,因此它们在时间旅行调试器中会以完全相同的方式重放。
多个摄像机
如果存在多个摄像机,使用世界中找到的第一个活跃摄像机。