Mobile Adventure RPG (Open Sourced)

Back to work on this.

Cleaned up the movement by having the player align itself to a grid during movement.

When the player moves along the x axis, it realigns along the y axis.

if state.is_pressed(hash("a")) then
	if not self.blocked.west then
		local pos = go.get_position()
		local dir = vmath.vector3(1, 0, 0)
		local new_pos = pos - dir

		new_pos.y = round(new_pos.y/8) * 8
		
		go.set_position(new_pos)
		
		sprite.set_hflip(".", true)
		play_animation(self, hash("player_walking"))	
		self.facing_dir = "west"			
	end
end

And vice versa.

elseif state.is_pressed(hash("w")) then
	if not self.blocked.north then
		local pos = go.get_position()
		local dir = vmath.vector3(0, 1, 0)
		local new_pos = pos + dir

		new_pos.x = round(new_pos.x/8) * 8
		go.set_position(new_pos)
		
		play_animation(self, hash("player_walking_north"))
		
		self.facing_dir = "north"

The round function:

local function round(n)
	return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end

This method prevents the player from getting snagged on obstacles as well as makes it easier for them to position themselves to enter tight spaces.

Example:
(Notice how when the character is unable to enter a passage, the player doesn’t have to “fight” to find the pixel perfect vector to enter, because the game aligns the player to the grid)

To prevent movement across tiles, I’m using tile map collisions. The player has 4 collisions that use triggers to block the movement.

if message_id == hash("trigger_response") then
		if sender.fragment == hash("player_north") then
			if message.enter then
				self.blocked.north = true
			else
				self.blocked.north = false
			end
		elseif sender.fragment == hash("player_south") then
			if message.enter then
				self.blocked.south = true
			else
				self.blocked.south = false
			end
		elseif sender.fragment == hash("player_east") then
			if message.enter then
				self.blocked.east = true
			else
				self.blocked.east = false
			end
		elseif sender.fragment == hash("player_west") then
			if message.enter then
				self.blocked.west = true
			else
				self.blocked.west = false
			end
		end
	end

The following info was helpful for doing this:

4 Likes