ursina_tutorials icon indicating copy to clipboard operation
ursina_tutorials copied to clipboard

meshcraft - block names, as a values to a td dict, for block identification +play step sound for the specific block

Open LameniRuz opened this issue 4 years ago • 2 comments

Hello, Thank you for this great tutorial, its a lot of fun, i have a little suggestion 1) change "t" to a specific block type name for example - ("grass") for example during assignment in the getBlock this.td[(floor(x),floor(y),floor(z))] = blockType to use this names later for a specific block sound, block yield

we can check if there is a block on this position like this

 block = terrain.td.get( (floor(x), floor(y+i), floor(z)) )
         if block and block != "g": #"t" now block type
                 #Do something
                 #for ex, Play sound for this block 
                 play_step_sound(block)

2) example how we can play step sound for the specific block

  • main.py
grass_audio = Audio('step.ogg',autoplay=False,loop=False)
snow_audio = Audio('snowStep.mp3',autoplay=False,loop=False)
sounds_for_blocks = { block_names.grass:  grass_audio, block_names.snow: snow_audio}
pitches_min_dict = { block_names.grass:  0.7, block_names.snow: 0.3}

def play_step_sound(block):
    step_sound = sounds_for_blocks.get(block)
    min_pitch = pitches_min_dict.get(block)
    if not step_sound: step_sound = grass_audio 
    if not min_pitch: min_pitch = 0.7
    if step_sound.playing == False:
        step_sound.pitch = random() + min_pitch
        step_sound.play()

i don't know how we can separate sound control from the main function it needs app = Ursina() loaded to work

there is block type name class, used in the previous code

  • config.py
class BlockTypeNames:
    def __init__(self):
        self.grass = "grass"
        self.soil = "soil"
        self.stone = "stone"
        self.ice = "ice"
        self.snow= "snow"
block_names = BlockTypeNames()

PS: Sorry for my bad English, And once again thank you for this cool series

LameniRuz avatar Jan 26 '22 18:01 LameniRuz

Thank you for this, Domenic.

This is very close to what I am planning to do in terms of the values stored in the terrain dictionary.

I like your idea of using a config.py and BlockTypeNames class to 'enum' the types of block -- this will be useful across the project. I think I need to look into how to best store the 'sounds for blocks' etc. Like the idea of using a dictionary here.

Thanks so much for taking the time to write this. I'll be implementing this in upcoming tutorial videos!

RedHenDev avatar Jan 27 '22 12:01 RedHenDev

Thank you for the reply, I'm glad it can be useful I will add another issue, about red dot height fix and how to place blocks in different directions O, you have already uploaded another tutorial, i will be watching it next, thanks

LameniRuz avatar Jan 27 '22 16:01 LameniRuz