The Open Game Component Library is a simple, easy-to-learn library for 2D-Game-Programming using JavaScript. It contains different components which makes it easy to code a simple 2D-Game in just a few lines.
- download the file 'ogc.js' from https://github.com/mattes2008/OpenGameComponentLibrary/blob/master/src/ogc.js
- include it as a script-tag
For Example:
Directory-Structure:
Project
|index.html
|_js
| |ogc.js
| |scripts.js
| |___________
|_css
| |style.css
| |___________
|_____________
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Project</title>
<link rel="stylesheet" href="css/style.css">
<script src="js/ogc.js"
</head>
<body>
<script src="js/scripts.js"></script>
</body>
</html>
To initialize the library call the 'init'-function with the stage's parent object:
ogc.init(destination);
After initialization, select the components you want to use in your program. For example:
const {figure, stage} = ogc;
By running this line, you are abled to call the components by their name instead of ogc.name:
componentName
instead of
ogc.componentName
The components are the central elements of ogc. These components make it easy for you to code your game and make it alive.
Stage is a component of ogc. It is the area in which the game take place. The stage is created by calling the 'init'-method. After creating, you can call the stage's properties by accessing the stage and the property.
ogc.init(destination); //initialize ogc
ogc.stage.property //access property
size
-width
width of the stage -height
height of the stageresize(width, height)
resize the stagegravity
gravity of stagebackground
list
list of backgroundsactual
number of actual backgroundadd(type, value, title)
add a backgroundremove(index)
remove a backgroundto(index)
chance to specific backgroundnext()
next backgroundlast()
last background
event
list
list of event listenersadd(event, method, title)
add an event listenerremove(title)
remove an event listener
toPixel(x, y)
convert coordinate to pixeltoCoordinate(x, y)
convert pixel to coordinateelement
stage elementupdate()
update stage
create(name, costumes)
all
Figure is a component of ogc. Figures are elements on the stage which can be used as players, enemies, displays and many more. To create a figure you have to call the 'create'-function of figure with the name and an array of different costumes. After creating, you can call the figure by the 'all' property and the figure's name.
ogc.figure.create(name, costumes); //create figure
ogc.figure.all.name //access the figure
Every figure has different properties which help you to interact with them. Your calling them by access the figure and the property name.
ogc.figure.create(name, costumes); //create figure
ogc.figure.all.name.property //access the property
position
x
position on the x-axisy
position on the y-axis
move(direction, length)
move the figure about a length into a directionmoveTo(x, y)
move the figure to an x and y coordinatemoveToObject(object)
move the figure to another objectmoveRelative(direction, length)
move the figure relative to the figure's rotationgravity
weight
figure specific weightenable(weight)
enable gravity with a figure specific weightdisable()
disable gravity for the figure
rotation
rotation of figurerotate(value, type)
chance the figures rotation to or about a specific valuerotateRelative(value)
rotate the figure relative to the figure's rotationrotationEnabled
rotation type of the figuredistance(object)
calculate the distance to an objectcollision
collider
figure specific collision radiuswithObject(object, radius, objectRadius)
check for a collision with a specific objectadd(title, object, method, radius, objectRadius)
add a collision listenerremove(title)
remove a collision listenerlist
list of collision listeners
size
width
width of the figureheight
height of the figure
resize(width, height)
resize the figureresize(value)
resize the figure by multiplying with a specific valuevisibility
state
state of visibilityshow()
change visibility to truehide()
change visibility to falsetoggle()
toggle visibility
costumes
list
list of costumesactual
number of actual costumeadd(src)
add a costumeremove(index)
remove a costumenext()
next costumelast()
last costumeto(index)
chance to specific costume
event
list
list of event listenersadd(event, method, title)
add an event listenerremove(title)
remove an event listener
clone(onCreation)
create a clone of the figureelement
figure elementmessage
costume eventssend(keyword, data)
send a message to every connected listeneron(keyword, method)
created a message listener
update
update figureremove
remove figure
player(name, costume)
2d player figurejumpNRunPlayer(name, costume)
2d jump-n-run figurevariableDisplay(name, referenceVariable)
variable-display figure
Audio is a component of ogc. Audio allows you to play audio in your game. You can access audio after calling init. You can access the properties of audio by accessing audio and the property.
ogc.init(destination); //initialize ogc
ogc.audio.property //access property
list
list of audio titlesadd(title, src)
add an audio titleremove(title)
remove an audio titleplay(title)
play an audio titlepause(title)
pause an audio titlesetAttribute(title, attribute, value)
change settings of an audio titlequick
volume
quick-audio volumeplay(src)
play audio quickly
Storage is a component of ogc. Storage can be sed to store data during short and long times. You can access storage after calling init. You can access the properties of storage by accessing storage and the property.
ogc.init(destination); //initialize ogc
ogc.storage.property //access property
sessionStorage
add(key, value, override)
create a key in 'sessionStorage'read(key)
get a key in 'sessionStorage'remove(key)
remove a key in 'sessionStorage'list()
list 'sessionStorage'clear()
clear 'sessionStorage'
localStorage
add(key, value, override)
create a key in 'localStorage'read(key)
get a key in 'localStorage'remove(key)
remove a key in 'localStorage'list()
list 'localStorage'clear()
clear 'localStorage'
Variables is a component of ogc. It is used to store data until a reload. It can be accessed by accessing variables and the property after calling init.
ogc.init(destination); //initialize ogc
ogc.variables.property //access property
storage
collection of variables, stored in variables componentset(key, value, override)
change or create a variable, stored in variables componentget(key)
read a variable, stored in variables componentremove(key)
delete a variable, stored in variables componentclear()
delete all variables, stored in variables component
Message is a component of ogc. You can use it for creating senders and listeners for specific event.
ogc.init(destination); //initialize ogc
ogc.message //access listener list
ogc.figure.create("name", [costumes]); //create a figure
ogc.figure.all.name.message //access a property