0
\$\begingroup\$

I am making a mining styled game and am looking to make variables that effect how fast you are able to mine the block. This is the idea I have currently got but it does not wait for the timer to complete before mining, has anyone got a better way to do this?

        if (MinePlayerCollision() && newState.IsKeyDown(Keys.D))
        {
            MiningElapsed = (float) gameTime.TotalGameTime.Seconds;

            if (MiningEff <= MiningElapsed)
            {
                BlockMined = true;
                MineReset = false;

                MiningElapsed = 0;
            }                
        }
\$\endgroup\$
4
  • \$\begingroup\$ How do you define and set MiningEff? \$\endgroup\$
    – Kromster
    Commented Oct 15, 2014 at 4:50
  • \$\begingroup\$ It's a variable declared at 8 default (hopefully 8 second wait) then is changed in a if statement to represent times down to 1 ( 1 second wait) \$\endgroup\$ Commented Oct 15, 2014 at 5:47
  • \$\begingroup\$ You should add that to your questions code. \$\endgroup\$
    – Kromster
    Commented Oct 15, 2014 at 7:53
  • \$\begingroup\$ Also "D" as a hardcoded Dig button? please allow customizing the keybindings \$\endgroup\$ Commented Oct 15, 2014 at 9:51

2 Answers 2

1
\$\begingroup\$

A health/damage mining system would be much more flexible and should be easier to implement than a fixed, time-based system. Basically, assign each block a health value, and your pickaxe a "mining damage" value, and whenever the pick strikes the block, you subtract its damage from the health of the block. When the block's health goes to zero or below, the block is mined. In order to achieve a certain time span until a certain block of ore is mined, then set the block's health equal to damage per pick strike * pick strikes per second

Advantage: You can re-use the existing health system for all your other entities, if you have one, and you can much more easily introduce and balance kinds of ores that are easy/hard to mine and pickaxes of different strengths. You can also give your players more flexibility in what tools they use for different tasks. For example, why not allow your players to mine ores with dynamite, i.e. explosive damage, instead of only pickaxes? A health/damage system allows you to do that quite easily.

\$\endgroup\$
1
  • \$\begingroup\$ I have decided to go with the HP method and I've added in some TNT and things so its working wonderfully thanks! \$\endgroup\$ Commented Oct 17, 2014 at 8:24
0
\$\begingroup\$

So you are on the right track but using not understanding what some of the parts of your code do.

GameTime.TotalGameTime Property

Game time since the start of the game.

GameTime.ElapsedGameTime

Elapsed game time since the last update.

MiningElapsed is currently set to the number of seconds since your game started. As a result its always going to become and stay larger than MiningEff. What you want to use is the time delta gameTime.ElapsedGameTime.

    if (MinePlayerCollision() && newState.IsKeyDown(Keys.D))
    {
        //Add (not set) the time since last update. Also you update many times per
        //second. Because of this you need a much higher resolution if you don't want
        //to use a `TimeSpan` at least use milliseconds. 
        MiningElapsed.add(gameTime.ElapsedGameTime); 

        if (MiningElapsed.Seconds >= MiningEff)
        {
            BlockMined = true;
            MineReset = false;

            MiningElapsed = TimeSpan.Zero;
        }                
    } else {
        //You have to reset the elapsed time or else when you stop and restart
        //mining it will already be partially completed.
        MiningElapsed = TimeSpan.Zero;
    }

P.S. I agree with Hackworth it is better to use a HP approach.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .