Python Ursina:通过实体的坐标销毁实体



我想有一个函数,说destruction(x,y,z)破坏实体放置在坐标(x,y,z)(如果有一个)。当然,问题是要有一个getter来返回某个坐标对应的Entity。

有什么办法吗?

只需在其中应用一个逻辑

coordinates = "10,10,10"
blocks = ["block1","block2","block3"]
blockspos = ["5,1,5", "10,10,10", "6,1,6"]
for b in blocks:
if blockspos[blocks.index(b)] == coordinates:
print('the block at coordinates is ' + b)

idk如何通过字符串名称破坏块,但你可以使用entity.disabledestroy(entity)来破坏实体,所以如果你想通过字符串名称破坏实体,那么应用这些代码。希望这能奏效。很抱歉我回复晚了。

你可以这样做:

(记住你想要摧毁的实体必须有一个碰撞器)

def destruction(position: Vec3):
collider_entity = Entity(
model = “cube”,
collider = “box”,
visible = False,
# The scale of the collider entity must be smaller than the scale of the entity you want to destroy, otherwise the collider entity might detect other entities that you don’t want to destroy. In this case, I’m assuming the entity you want to destroy has a scale of Vec3(1, 1, 1), so I’ll make the collider entity have a scale of Vec3(0.5, 0.5, 0.5). If you want, you can pass the size of the entity you want to destroy as an argument to the function, then set the size of the collider entity to the size of the entity divided by 2.
scale = Vec3(0.5, 0.5, 0.5),
position = position
)
destroy(collider_entity.intersects(ignore = [collider_entity]).entity)
destroy(collider_entity)

这是我在尝试解决自己的问题时发现的一个非常优雅的解决方案:

下面是我在程序中使用的代码:

# suppose you are generating lots of entities:
for z in range(16):
for y in range(7):
for x in range(16):
if y >= 6:
#voxel is just an entity
voxel = Voxel(position=(x, y, z), texture=grass_texture)
if x == 8 and y == 6 and z == 8: """just check the coordinates of your entity while generating the entities themselves"""
destroy(voxel)#you can also save the entity in a variable if you don't want to destroy it while generating
elif y >= 4:
voxel = Voxel(position=(x, y, z), texture=dirt_texture)
elif y >= 2:
voxel = Voxel(position=(x, y, z), texture=stone_texture)
else:
voxel = Voxel(position=(x, y, z), texture=deepslate_texture)

但是如果你想通过输入来销毁它,你可以在实体类定义中添加输入函数,像这样:

#my entity or button (both count as entities) 
class Voxel(Button):
def __init__(self, position=(0, 0, 0), texture=grass_texture):
super().__init__(
parent=scene,
position=position,
model="assets/block",
origin_y=0.5,
texture=texture,
color=color.color(0, 0, random.uniform(0.9, 1)),
scale=0.5,
collider="box",
)
def input(self, key):
if self.hovered:
if key == "left mouse down":
punch_sound.play()
if block_pick == 1:
voxel = Voxel(
position=self.position + mouse.normal, texture=grass_texture
)
if block_pick == 2:
voxel = Voxel(
position=self.position + mouse.normal, texture=stone_texture
)
if block_pick == 3:
voxel = Voxel(
position=self.position + mouse.normal, texture=brick_texture
)
if block_pick == 4:
voxel = Voxel(
position=self.position + mouse.normal, texture=dirt_texture
)
if block_pick == 5:
voxel = Voxel(
position=self.position + mouse.normal, texture=glass_texture
)
if block_pick == 6:
voxel = Voxel(
position=self.position + mouse.normal, texture=deepslate_texture
)
if block_pick == 7:
voxel = Voxel(
position=self.position + mouse.normal, texture=granite_texture
)
if block_pick == 8:
voxel = Voxel(
position=self.position + mouse.normal, texture=log_texture
)
if block_pick == 9:
voxel = Voxel(
position=self.position + mouse.normal, texture=planks_texture
)
#you can follow this example using the self argument to destroy it:
if key == "right mouse down":
punch_sound.play()
destroy(self)

部分答案:我找到了一种方法,通过使用全局dict(),说BLOCKS,由我的脚本创建的每个块:

BLOCKS = dict()
def build_block(x, y, z):
BLOCKS[(x,y,z)] = Voxel(position = (x, y, z), ...)
def destroy_block(x, y, z):
if (x,y,z) in BLOCKS:
destroy(BLOCKS[(x,y,z)])
del(BLOCKS[(x,y,z)])

仍然需要找到如何破坏用鼠标左键创建的块。

最新更新