howto move avatar only from tile to tile

16 views
Skip to first unread message

joa.c...@gmail.com

unread,
Apr 22, 2015, 3:12:20 PM4/22/15
to gummw...@googlegroups.com
Hello again Gummworld and everybody here,

I've been trying to be able to move my avatar only from tile to tile, but I have no succees!

what I already have done:
I've a map created with Tiled, with tiles of 60x60 px
I load it with gummlib

what I want:
is to move my avatar only from tile to tile,
I don't want to move to any x, or y coordinates inside map, I only want to stay and go, from the x,y center coordinates of a tile, to another tile.

what I have tried :-):
I've derived spatialhash to have cells asociated to the map get their center coordinates,etc. I get the cell where the avatar is, and I move to another cell
The problem is cells size is not the same as the tiled size. (not in my game I don't know why) So I move, but in a space bigger than the tiles size.

well, I'm not from any english speaking country, SO it's difficult to me to explain it better.
thanks if anybody here could give some hint to get this.

Joa.

bw

unread,
Apr 22, 2015, 11:19:25 PM4/22/15
to gummw...@googlegroups.com
Hey there.

In order to provide accurate help we would need to see some code. If you have a source control repo or dropbox that'll work fine.

Also, I don't have a clear what kind of game you're making.

If it is a scrolling game, you need to modify the rect which belongs to the camera target.

If it is not a scroll game then you don't need to update a camera target (because it would be stationary).

Hope that helps for now. If you need more help, please provide a link to your code.

Cheers,

Gumm
--
You received this message because you are subscribed to the Google Groups "Gummworld2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gummworld2+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

joa.c...@gmail.com

unread,
Apr 29, 2015, 4:20:40 AM4/29/15
to gummw...@googlegroups.com
Hi Gumm,

this is the link from the github repository:
https://github.com/gentooza/Freedom-Fighters-of-Might-Magic/tree/dev
(I'm currently working in the dev branch, so you'll have to do a checkout afterwards the cloning)

I think I've finnally done it, but I'm sure it's not he best way to do it

I'm trying to do a Heroes of Might & Magic like game, so, the avatar is the camera target.
I've been sutdying your examples provided with gummlib, and I like 17_load_and_use_world.py, but I want to limit the movement of my "hero" to only tile to tile.

I'm using to get cells coordinates, etc. a derived class of spatialhash with only a new function to give me the cell id by providing row and col.

pd:don't use the mouse when testing, is broken for now :-e!

bw

unread,
Apr 29, 2015, 1:49:09 PM4/29/15
to gummw...@googlegroups.com
Hey, I recognize this game. The email address threw me off a little. It's good to see activity on the mailing list. :)

I noticed one problem. It isn't related to your question, but I think you'll want to solve it. Not sure if this was added by you, or a bug that I later fixed.

gummworld2/toolkit.py:
import urllib.request, urllib.parse, urllib.error
(These raise an exception in Python 2.7.)

To get the avatar behavior that you seem to want, I did the following. These changes don't check the bounds of the world. I just wanted to keep it simple so it is easy to understand.

1. In gameEngine.__init__:
change: self.step = None
to: self.step = Vec2d(0, 0)

2. Changes to gameEngine.update_keyboard_movement (see the changes marked "Gummchange" here):
    #keyboard movement between cells
    def update_keyboard_movement(self):
        # Gummchange
        if self.move_x == 0 and self.move_y == 0:
            return
        print('CALCULATING MOVE_TO')
        # end Gummchange

        # Current position.
        camera = State.camera
        wx, wy = camera.target.position
        cell_id = self.world.index_at(wx,wy)
        row,col = self.world.get_cell_grid(cell_id)
        #print("actual position: ",wx,wy," inside the cell: ",cell_id," row and col: ",row,col)
        #new situation of new cell
        if(row == 0 and self.move_y < 0):
           new_row = row
        else:
           new_row = row + self.move_y
        if(col == 0 and self.move_x < 0):
           new_col = col
        else:
           new_col = col + self.move_x
        #does it exist?
        cell = self.world.get_cell_by_grid(new_col,new_row)
        if(cell == None):
           #doesnt exist
           return
        else:
           cell_id = self.world.index(cell)
           self.new_x,self.new_y = self.world.get_cell_pos(cell_id)
           row,col = self.world.get_cell_grid(cell_id)
           self.new_x += self.cell_size/2
           self.new_y += self.cell_size/2

           # Gummchange
           self.move_to = Vec2d(self.new_x, self.new_y)
           self.step = Vec2d(self.move_x, self.move_y)
           #print("a la position: ",self.new_x,self.new_y," inside the cell: ",cell_id," row and col: ",row,col)
        # self.step.clear()
        # end Gummchange

3. Rename gameEngine.update_camera_position (or comment the method to save your original code).

4. Add the following update_camera_position method:
    # Gummchange
    def update_camera_position(self):
        # if move_to, then the camera needs to keep stepping towards the destination tile.
        if self.move_to:
            print('STEP pos{} -> dest{} by step{}'.format(
                tuple(self.camera.position), tuple(self.move_to), tuple(self.step)))
            camx, camy = self.camera.position
            stepx, stepy = self.step
            # Check if camx has arrived at the move_to point. If it has set stepx to 0.
            if camx == self.move_to.x:
                stepx = 0
            # Check if camy has arrived at the move_to point. If it has set stepy to 0.
            if camy == self.move_to.y:
                stepy = 0
            # If steps remain on the x or y axis, update the camera position. Else, the
            # camera/avatar is done moving, so set self.move_to = None.
            if stepx or stepy:
                self.camera.position += stepx, stepy
            else:
                self.move_to = None

The behavior is that when a key is pressed the avatar wants to move to the next tile. Then each update takes a small step towards the next tile until the avatar's position == the destination.

Hope this helps.

Gumm

joa.c...@gmail.com

unread,
Apr 30, 2015, 8:38:08 AM4/30/15
to gummw...@googlegroups.com


On Wednesday, April 29, 2015 at 7:49:09 PM UTC+2, Gummbum wrote:
Hey, I recognize this game. The email address threw me off a little. It's good to see activity on the mailing list. :)

 yeah! :-) I had to use my gmail account to suscribe and write here. I think it's a good place to exchange experiences!

it works smooth, it's simple and an elegant sollution! cool
I've added now the terrain edges, and the avatar animation.

thanks

Reply all
Reply to author
Forward
0 new messages