Odyc.js

🪤 Events

To add interactivity, Odyc.js provides a simple event system.
Events let you trigger actions or modify the game state when the player interacts with an element.

Events are defined inside templates.


🔎 Types of events

There are three types of events:

  • onCollide — triggered when the player collides with the element
  • onEnter — triggered when the player enters a tile containing the element
  • onLeave — triggered when the player leaves a tile containing the element
createGame({
	templates: {
		x: {
			onCollide() {
				alert(1)
			},
			onEnter() {
				alert(2)
			},
			onLeave() {
				alert(3)
			}
		}
	}
})

🎯 The target of the event

When an event is triggered, the affected object is passed as a parameter to the function. You can use it to modify the object dynamically or even remove it.

createGame({
	templates: {
		x: {
			onCollide(target) {
				target.remove()
			}
		}
	}
})

📋 Available properties

Property / MethodTypeDescriptionRead-only
solidbooleanMakes the object passable or notNo
visiblebooleanShows or hides the objectNo
spritenumber | stringChanges the appearance of the objectNo
soundstring | objectChanges the sound played on interactionNo
dialogstring | string[]Modifies the text displayed in the dialog boxNo
endstring | string[]Triggers a custom game endingNo
symbolstringThe character representing the object in the mapYes
position[number, number][x, y] coordinates of the object on the mapYes
onCollidefunctionCalled when a collision with the object occursYes
onEnterfunctionCalled when the player enters the object’s cellYes
onLeavefunctionCalled when the player leaves the object’s cellYes
remove() => voidRemoves the object

Example: change a property

Let’s create a character that says “Hello” the first time, and “Hello again” next time:

createGame({
	templates: {
		x: {
			dialog: 'Hello',
			onCollide(target) {
				target.dialog = 'Hello again'
			}
		}
	}
})

Remove an object

To make an object disappear when touched, use the remove() method:

createGame({
	templates: {
		x: {
			dialog: 'Hello',
			onCollide(target) {
				target.remove()
			}
		}
	}
})