6

I am trying to create a simple board game using libGDX. Just that you have a rough idea of what I'm trying to do, imagine Bejeweled (though mine of course is not as complex).

The game involves a board with cells as squares. Depending on the level, this grid has a different number of cells, like 6x6 or 8x8. I also want to include some nice animation for switching the position of two neighboring cells (like in Bejeweled). Of course there also need to be some buttons on the screen.

My question is: What is the best way to do this? Shall I use a stage and then tables for the grid? Can I then still easily make an animation (using the Universal Tween Engine)? Or is it better to draw the Sprites individually? Or is there another completely different way of approaching this?

Thank you for your answers,

Cheers,

Tony

tomet
  • 2,416
  • 6
  • 30
  • 45

2 Answers2

7
Sprite square = new Sprite(new Texture("texture"));

Render

float squareWidth = camera.viewportWidth / squaresOnWidth;
float squareHeight = camera.viewportHeight / squaresOnHeight;
square.setWidth(squareWidth);
square.setHeight(squareHeight);
batch.begin(); `
for(int y = 0; y < squaresOnHeight; y++){
    for(int x = 0; x < squaresOnWidth; x++){
        square.setX(x * squareWidth);
        square.setY(y * squareHeight);
        square.draw(batch);
     }
 }
 batch.end();

This should output a grid of textures, not tested.

If you want to create smooth animation you should definitely look into UniveralTweenEngine, here's a demo of what it can do : http://www.aurelienribon.com/universal-tween-engine/gwt/demo.html

If you want the grid in buttons instead.

OrthoGraphicCamera camera = new OrthoGraphicCamera();
camera.setToOrtho(false, yourViewportWidth, yourViewportHeight);
camera.translate(xPos, yPos);
Stage stage = new Stage(your wanted stage width, your wanted stage height, false, batch);
stage.setCamera(camera); 
for(int y = 0; y < buttonsOnHeight; y++){
    for(int x = 0; x < buttonsOnWidth; x++){
       stage.addActor(new TextButton("" + x + y * buttonsOnWidth, textButtonStyle); 
    }
 }

The render

float buttonWidth = camera.viewportWidth / buttonsOnWidth;
float buttonHeight = camera.viewportHeight / buttonsOnHeight;
for(int y = 0; y < buttonsOnHeight; y++){
    for(int x = 0; x < buttonsOnWidth; x++){
        TextButton button = stage.getActors().get(x + y * buttonsOnWidth);
        button.setX(x * buttonWidth);
        button.setY(y * buttonHeight);
        button.setWidth(buttonWidth);
        button.setHeight(buttonHeight);
     }
 }

Then draw the stage, note that you should stop any batch that's currently running because stage has it's own batch.begin() and batch.end(). You could start your batch again after stage.draw();

 stage.act(delta);
 stage.draw();
Robert P
  • 9,398
  • 10
  • 58
  • 100
thetheodor
  • 248
  • 2
  • 22
  • Allright, so you don't use a stage. But how can I add buttons without a stage? Or is it possible to have a stage that only covers parts of the screen? – tomet Jan 30 '14 at 09:17
  • Thank you for your effort. I want both a grid of Sprites and some buttons below. So I need to render both somehow. – tomet Jan 30 '14 at 12:44
  • I have school to do right now, if I try to create an answer tomorrow including the sprites and buttons? Around 5pm GMT +1? – thetheodor Jan 30 '14 at 13:45
  • @fritzi2000 i think the part of your question is allready answered in your other question as you now (hopefully) know how to use camera, stage and spritebatch. If not just write a comment to your other question. @thetheodor Your answer seems correct, but i would not use `Gdx.graphics.getWidth(), Gdx.graphics.getHeight()` as viewport. If you do so you have to do everything in pixels and you depend on the window size. If you use a default, fixed value you have your own world coordinates, which get scaled up to fit the gamewindow. – Robert P Jan 30 '14 at 13:57
  • I have somehow programmed Gdx.graphics.getWidth/Height in my mind to always use it :), will edit the answer. Thanks! – thetheodor Jan 30 '14 at 15:33
  • No problem. It just helps to have a) Resolution independent code and b) you can have your own unit (lets say meters for example) and you can calculate things in meters instead of pixels. It makes things a lot easier in my oppinion :) – Robert P Jan 30 '14 at 15:37
  • @thetheodor i have seen you use camera.viewportHeight/Width. camera.viewPortWidth is the current viewport width. You should set it to a value you want to have, for example 80 to have xCoordinates from 0 to 80 where 0 is left and 80 is right border of the gamewindow. Hope you understood (: – Robert P Jan 30 '14 at 15:42
0

To have a grid you could and should use camera:

OrthographicCamera cam = new OrthographicCamera(8,8);

You tell the camera to have a viewport of 8 x and 8 y. The cameras (0,0) point is in the middle of the screen.

To have it at the left bottom you need to set its position to

cam.translate(camWidth / 2, camHeight / 2); 

Now you can add your sqares at sqare.setX(0) for sqares on the bottom line or sqare.setY(3) to add it on the 4rd row from left to right. For the animations you could also use Actions, which allow you to add different movements to an actor and let him perform them over time. Example:

actor.addAction(Actions.parallel(Actions.moveTo(float x, float y, float duration), Actions.rotateTo(float rotation, float duration)));

With this code sample you tell your actor to move from his position to (x,y) in duration seconds and while he moves to this position he rotates from his current rotation to rotation in duration seconds. Hope this helps

Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
Robert P
  • 9,398
  • 10
  • 58
  • 100
  • there's not a method call setPosition, it's named translate and does the same thing. Also, instad of manually setting the camera to 0, 0, you can call setToOrtho(); – thetheodor Jan 30 '14 at 13:50
  • The cameras position is 0/0 by default as much as i know. This is actually only pseudocode to let peaople know how it works. I don'T remember exact method names, Eclipse helps me a lot :P Thanks for your comment i will edit my answer! – Robert P Jan 30 '14 at 13:53
  • Eclipse does really help a lot, had to test code there before posting! :D – thetheodor Jan 30 '14 at 15:32
  • Haha nice :P I upvoted your answer, but please read my comment there. – Robert P Jan 30 '14 at 15:33