How do you make a mining mechanic for a mining game?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DevDeer

Hi, I’m making a game where you can mine ores, explore caves and more. I’m a beginner in Godot so when it comes to this stuff, I can’t do it. I want this mining mechanic to remove tiles from a tilemap while also having a cracking animation. Could anyone help me do this? (it’s a 2D game btw)

Imgur
Imgur

by animations do you mean hit once and the block changes and you have to hit it again for it to break or an actual animations of it braking

lewis glasgow | 2021-08-25 17:16

I mean hitting it once and the block changes and you have to hit it again for it to break

DevDeer | 2021-08-25 17:47

easy just check if the tile id is 0 if it is change it to 1 then if its one get rid of it
you might need to change the 0 and 1 around

var block_type
if is_colliding():
    var point = get_collider().world_to_map(get_collision_point())
    if .get_collider().get_cellv(point) == 0:
        get_collider().set_cellv(point,1)
    elif .get_collider().get_cellv(point) == 1:
        get_collider().set_cellv(point,-1)

lewis glasgow | 2021-08-25 18:49

I’m having an error that says “The method get_collision_point isn’t declared in the current class”. Also do you know a way you can do it so that it has more tiles than once? For example: when you hit the block once, it shows the first crack, then when you hit it again it shows the second crack and finally when you hit it the third time it displays the third crack and then it would break.

DevDeer | 2021-08-25 19:13

make shore you reference the raycast node when dealing with collisions

lewis glasgow | 2021-08-25 20:16

Ok, so I referenced the ray cast node (as you can see on the code) but I’m still getting an error saying “Nonexistent function get_collider”. (Also I don’t know if I referenced it right)

	if Input.is_action_pressed("click"):
	state = MINE

#The code above is for the if clicked action and stuff…
if $RayCast2D.is_colliding():
var point =$RayCast2D.get_collider().world_to_map($RayCast2D.get_collision_point())
if .get_collider().get_cellv($RayCast2D.point) == 0:
$RayCast2D.get_collider().set_cellv($RayCast2D.point,1)
elif .get_collider().get_cellv($RayCast2D.point) == 1:
$RayCast2D.get_collider().set_cellv($RayCast2D.point,-1)

DevDeer | 2021-08-25 21:02

as you may know its really hard to debug without the project so if you it downloadable i will get to the bottom of it

lewis glasgow | 2021-08-25 21:16

Sorry for the late response, here you go: The mine – Google Drive

(Also the code is in the player script)

DevDeer | 2021-08-26 06:46

there you are all patched up just make shore all destroyable things have collision shapes

The Mine

lewis glasgow | 2021-08-26 12:04

Thanks so much! I really appreciate the help you’ve done for the game. Would you like to be credited in the game?
(Edit: Do you know how to make it so that when the tile is broken, it would update it?

like this:
Imgur: The magic of the Internet
Imgur: The magic of the Internet

DevDeer | 2021-08-26 16:49

no thanks and here it is with tile update thing

lewis glasgow | 2021-08-26 21:01

:bust_in_silhouette: Reply From: lewis glasgow

have a raycast2D and make shore its active and it rotate towards the mouse using look_at(get_global_mouse_position) this also works with mobiles so don’t worry about that

next make it so when you press the attack button this happends

var block_type
if is_colliding():
	var point = get_collider().world_to_map(get_collision_point())
	if .get_collider().get_cellv(point) != -1:
		block_type = .get_collider().get_cellv(point)
		get_collider().set_cellv(point,-1)

not shore what to do with animations though if i find a way i will let you know

Oh cool, yeah if you can also find a way for the animation please comment it there again. Thanks for the help!

DevDeer | 2021-08-25 17:09

Also on this code, I’m having an error that says “The method “is_colliding” isn’t declared in the current class”.

DevDeer | 2021-08-25 18:15

Why are you using colliders?
The game Is 2d, you can get the click position with a simple

func _input(event):
 if event.is_action_pressed ("mouse_left"):
  clicked_tile=world_to_map(get_global_mouse_position ())

Andrea | 2021-08-25 21:48

getting just the global mouse mouse pos means you can delete any block on the screen where as a raycast limits it to a range and block order (if there’s a block in the way it will get rid of it first which is more realistic)

lewis glasgow | 2021-08-25 22:08

For changing the tiles visual appearance as it breaks this might help - https://forum.godotengine.org/11223/is-it-possible-to-change-animate-tilemap-cells

Asthmar | 2021-08-26 00:27