Defining a chapter

The goal of this project has been loading content automatically and handling multi-part areas, so it's important to include features like quest definitions and to specify which area in a chapter the player will begin in.

Getting ready

Create a file in the chapter/1 folder of the project directory, contents, and open it in a text editor.

Getting on with it

This chapter is simple and needs only two pieces of information.

Specifying a chapter's beginning and end

On the first line of the chapter content file, specify the chapter start:

start:

The chapter will begin in the desert_camp map, on a square next to the tent decoration:

start:  desert_camp  8,6

On the next line, we'll detail the chapter's goal: the collection of eight coin objects:

start:  desert_camp  8,6
goal:    coin  8

Launching a selected chapter

Save the content file and open game.lua. This module creates the game object and attaches its relevant functions. The game:Begin() function is intended to launch a particular chapter. In order to do this, it will read the content file to place the character in the right scene and note which accomplishments it needs to track on the player to determine when the chapter is over. Start by looping over the lines of the content file for the specified chapter:

  function self:Begin(chapter)
    local target, x, y
    for line in io.lines(directory.."/contents") do
      local action, details = line:match("^([^:]+):%:%s+(.+)$")
    end
  end

When a start line is found, record which scene and location it needs to go to; we'll finish processing the file before launching the actual transition:

    for line in io.lines(directory.."/contents") do
      local action, details = line:match("^([^:]+):%:%s+(.+)$")
      if action == 'start' then
        target, x, y = details:match("^(%w+)%s+(%d+)%,(%d+)")
        x, y = tonumber(x), tonumber(y)
      end
    end

When a goal line is found, record the type of goal being sought and how many are required:

      if action == 'start' then
        target, x, y = details:match("^(%w+)%s+(%d+)%,(%d+)")
        x, y = tonumber(x), tonumber(y)
      elseif action == 'goal' then
        local kind, count = details:match("^(%w+)%s+(%d+)")
        self.GoalKind, self.GoalCount = kind, tonumber(count)
      end

Tracking goal progress

While we're processing this file, go to the Proceed function in the gotoScene call further down, and register the game to receive events that indicate the player gained a new item:

self.Player = character
    self.Player:addEventListener('Gained', self)
    UI:AttachPlayer(self.Player)

Add a function to the game object to respond to user when these events are received:

  self.isVisible = false
  function self:Gained( event )
  end
  function self:Begin(chapter)

This handler will compare the received prize to the desired goal item to determine if it should be counted:

  function self:Gained( event )
    if event.object == self.GoalKind then
      self.CountAcquired = (self.CountAcquired or 0) + 1
    end
  end

If the count has reached the needed number of items, the chapter is declared completed:

    if event.object == self.GoalKind then
      self.CountAcquired = (self.CountAcquired or 0) + 1
      if self.CountAcquired >= self.GoalCount then
        self:dispatchEvent{name = 'Goal'; action = 'completed', kind = self.GoalKind}
        self:dispatchEvent{name = 'Chapter'; action = 'completed', index = self.Chapter}
      end
    end

What did we do?

While the separate map files in a chapter folder specify its various zones and goodies, we've added a file that indicates how and where the chapter should begin, and what special deeds are needed to finish it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.74.231