Mobile Adventure RPG (Open Sourced)

**Update: I’ve decided to make the code for this game publicly available. Anyone who wishes to contribute may do so. I think making the game code public would also help when I run into issues and asking for input. **

https://github.com/raymondduke/mobile-adventure-game-open

Hello (again). I was active here for a while and then took a break. I still find learning how to program games using Lua and Defold to be both exciting and challenging, so I’m taking some ideas I have into a game. The idea is to make a game like the original Zelda with a compelling story and fun gameplay.

7 Likes

Because the Legend of Zelda is a classic, there are a lot of resources available online.

Such as:

Another very useful case study of Zelda. Referencing this often when building this game.

Definitely looking forward to learning from these and implementing the mechanics in Defold.

7 Likes

Got the directions working and now I’m working on the animations. I also got a deflect system to work. I found another article that breaks down the mechanics of Zelda and that’s been helpful for setting up my game.

I’m making the sprites myself. They’ll be 16x16. I’m thinking of having different sprites for in/out of town.

3 Likes

There’s probably many ways to solve the direction issue.

I’d have a variable (e.g. self.animation_direction) that I would only update in input. For example:

if action_id == hash("w") then
		self.input.y = 1
		self.input.x = 0
		if action.pressed then
			self.animation_direction = hash("north")
			sprite.play_flipbook("#sprite", hash("player_walking_north"))
		elseif action.released then
		end
	end

Set it accordingly for each other direction.

Then when it’s time to set the idle animation:

    if self.moving then
		go.set_position(go.get_position() + self.dir * self.speed * dt)
    elseif self.animation_direction == hash("north") then

        --animation here

    --elseif self.animation_direction == hash("south") then

        --other animations here etc

	else
		sprite.play_flipbook("#sprite", hash("player_idle"))
	end

Does that make sense or have I misunderstood your issue? Nested ifs like the above would work and are probably a good way to start out, but for more elegance I’d be storing animation references in tables something like:

 sprite.play_flipbook("#sprite", idle_animations[self.animation_direction])
3 Likes

Need to touch up my art, but the animations are working. Thanks again.

3 Likes

Decided to make the code public. See the first thread. I’m going to clean up the posts to make it clearer what’s been done and what’s left to do.

This ongoing project is available here:

6 Likes

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.

Adventure Game - Defold Editor 2.0 2022-01-04 12-30-04

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:

3 Likes

Hey Raymond, its nice to know my grid workaround was useful to you. Good luck with the project!