• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nicholas Kong

Better way to write a code that simulates monster movement

5 posts in this topic

 
Here is my approach written in Java:
 
I am not a fan of hardcoding indices which is why I was looking for a better way of writing the code.
 

private int[] monsterMovementFrames = {100,200,300,400,500};
 
public void update()
{

            if(!isDead)
            {
                   counter++;
            }
 


 

            if(counter == monsterMovementFrames[0] || counter == monsterMovementFrames[2])
            {
                   state = ActionState.RUNNING;
 
            }
 
            if(counter == monsterMovementFrames[0] || counter == monsterMovementFrames[4] )
            {
                  direction = Direction.RIGHT;
 
            }
 
            else if(counter == monsterMovementFrames[1] || counter == monsterMovementFrames[3])
            {
                 state = ActionState.IDLE;
            }
 
            else if(counter == monsterMovementFrames[2])
            {
                 direction = Direction.LEFT;
 
            }
 
            if(counter == monsterMovementFrames[4])
            {
                 counter = 0;
            }

0

Share this post


Link to post
Share on other sites

Judging on the code snippet shown in the OP alone, when the monster dies for example when counter is exactly 100, so isDead is set to true, in the next update iteration the counter is not altered, and hence the first conditional matches, the state is set to RUNNING, and so the monster became a zombie monster ;)

 

To get rid of hard coded numbers, the usual way is to load a set of "instructions" from the resources. Mapping your use case, the data should then describe both when the behavior should be altered and to which behavior it should be altered. So let us assume a sequence of number tuples, where the one number denotes a moment in time and the other denotes an instruction. The sequence of behaviors is best ordered by the moment in time. E.g.:

enum Instruction {
     Idle =0,
     TurnRight,
     TurnLeft,
     Run,
     Restart
};
struct Behavior {
     uint16_t moment;
     uint16_t instruction;
};
Behavior const* Monster::behaviors;

After loading the array of behavior, a general routine can access and process it.

 

The above just makes your hard-coded behavior data-driven. A dynamic solution, i.e. where the monster may react on a given situation, would be modeled totally different, of course.

1

Share this post


Link to post
Share on other sites
If you don't want to hard-code... don't. Load up a data file that describes the monster, it's frames (I'm assuming those are animation frames), and which frames map to which abstract states.

Your code is somewhat confusing; are your frames defining the behavior of how/when the monster moves? Separate that data. Make code that determines when the monster moves. It sets abstract animation state. The animation code takes that abstract state and the animation data defined for the monster to select the proper frames of animation. The animations can be played without moving, moving can happen without animation, and they work together when appropriate.
0

Share this post


Link to post
Share on other sites


Judging on the code snippet shown in the OP alone, when the monster dies for example when counter is exactly 100, so isDead is set to true, in the next update iteration the counter is not altered, and hence the first conditional matches, the state is set to RUNNING, and so the monster became a zombie monster ;)

 

Nope isDead is only set to true when the monster life is less than or equal to zero but that code is not even posted. So you can't judge with your statement... =/ The counter reaching a certain value threshold only triggers behavior states that do not involving any death behavior on the monster's part.

0

Share this post


Link to post
Share on other sites


Your code is somewhat confusing; are your frames defining the behavior of how/when the monster moves?

 

It just specify "when" it is suppose to behave. You can think of the monster counting from 100 to 500 in-game. When it reaches a certain number, the monster behavior state changes.

 

The drawing code or "how" it gets displayed on screen is not posted because the drawing code is clean code. 

0

Share this post


Link to post
Share on other sites

 


Judging on the code snippet shown in the OP alone, when the monster dies for example when counter is exactly 100, so isDead is set to true, in the next update iteration the counter is not altered, and hence the first conditional matches, the state is set to RUNNING, and so the monster became a zombie monster ;)

 

Nope isDead is only set to true when the monster life is less than or equal to zero but that code is not even posted. So you can't judge with your statement... =/ The counter reaching a certain value threshold only triggers behavior states that do not involving any death behavior on the monster's part.

 

Please read again: My comment targets the case when isDead is set to true (regardless where in your code this happens) at a moment when counter is equal to any of the moments in time listed in monsterMovementFrames, for example 100. In such a case one of the conditional branches is true, and it is true forever because the counter is not changed anymore. That means although the monster is tagged as dead, its update() method still applies behavior to the monster over and over again! For sure, this is an edge case, but it can and will happen if enough runs are made.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0