简体   繁体   English

Python Ursina:通过其坐标摧毁一个实体

[英]Python Ursina : destroy an Entity via its coordinates

I'd like to have a function, say destruction(x,y,z) which destroy the Entity placed at coordinates (x,y,z) (if there is one).我想要一个函数,比如 destroy destruction(x,y,z) ,它破坏放置在坐标 (x,y,z) 处的实体(如果有的话)。 Of course, the problem is to have a getter which returns the Entity corresponding to some coordinates.当然,问题是要有一个 getter,它返回与某些坐标对应的实体。

Any idea to do this ?有什么想法吗?

Just apply a logic in it只需在其中应用一个逻辑

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 how to destroy block via string name but you can use entity.disable or destroy(entity) to destroy an entity so if you have idea to destroy entity via string name so apply these code. idk 如何通过字符串名称销毁块,但您可以使用entity.disabledestroy(entity)来销毁实体,因此如果您有想法通过字符串名称销毁实体,请应用这些代码。 Hope this work.希望这项工作。 Sorry about answer late.抱歉回复晚了。

This is a really elegant solution that I found while trying to fix my own issue:这是我在尝试解决自己的问题时发现的一个非常优雅的解决方案:

Here's the code that I have used in my program:这是我在程序中使用的代码:

# 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)

but if you want to destroy it by input you can add an input function in the entity class definition like this:但是如果你想通过输入来销毁它,你可以在实体类定义中添加一个输入函数,如下所示:

#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)

You could do something like this:你可以这样做:

(Keep in mind that the entity you want to destroy must have a collider) (请记住,您要销毁的实体必须有对撞机)

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)

Partial answer : i found a way by using a global dict() , say BLOCKS , of every blocks created by my script :部分答案:我找到了一种方法,方法是使用我的脚本创建的每个块的全局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)])

Still have to find how to destroy blocks created with a left click of the mouse.仍然必须找到如何销毁通过鼠标左键单击创建的块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM