Latest commit

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Game Utilities for Pixi (v3.9)

A library of helpful functions for making games and applications for the Pixi rendering engine.

(Important! This library targets Pixi v3.9, which is the most stable version of Pixi, and is the only version I can recommend using. This library will eventually be upgraded for Pixi v4 when the v4 branch matures.)

Setting up
distance: The pixel distance between two sprites
followEase: Ease a sprite to another sprite
followConstant: Follow a sprite at a constant rate
angle: Find the angle in radians between two sprites
rotateAroundSprite: Make a sprite rotate around another sprite
rotateAroundPoint: Make a point rotate around another point
randomInt: Get a random integer
randomFloat: Get a random floating point (decimal) number
wait: Set a delay before running the next task
move: Move a sprite by adding its velocity to its position
worldCamera: A camera for following objects around a large game world
lineOfSight: tells you whether a sprite is visible to another sprite

Setting up

Link to the gameUtilities.js file in your HTML document with a <script> tag, then create a new instance of the Game Utilities library in your JavaScript file like this:

letgu=newGameUtilities();

You can now access all the utility methods through the gu object.

Or, just copy and paster the function you need from the source file into your own project code.

distance

Find the distance in pixels between two sprites. Parameters: a. A sprite object. b. A sprite object. The function returns the number of pixels distance between the sprites.

letdistanceBetweenSprites=gu.distance(spriteA,spriteB);

If distance is calculated from the sprites' center points, assuming that the x/y anchor point is the sprite's top left corner. However, if a sprite's x/y anchor has been shifted, the distance will be calculated from that anchor point. All the Game Utility methods that depend on a distance calculation work in this same way (followEase, followConstant, rotateAroundSprite and angle).

followEase

Make a sprite ease to the position of another sprite. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The easing value, such as 0.3. A higher number makes the follower move faster.

gu.followEase(follower,leader,speed);

Use it inside a game loop.

followConstant

Make a sprite move towards another sprite at a constant speed. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The speed value, such as 3. The is the pixels per frame that the sprite will move. A higher number makes the follower move faster.

gu.followConstant(follower,leader,speed);

angle

Return the angle in Radians between two sprites. Parameters: a. A sprite object. b. A sprite object. You can use it to make a sprite rotate towards another sprite like this:

box.rotation=gu.angle(box,pointer);

rotateAroundSprite

Make a sprite rotate around another sprite. Parameters: a. The sprite you want to rotate. b. The sprite around which you want to rotate the first sprite. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundSprite(orbitingSprite,centerSprite,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

rotateAroundPoint

Make a point rotate around another point. Parameters: a. The point you want to rotate. b. The point around which you want to rotate the first point. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundPoint(orbitingPoint,centerPoint,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

randomInt

Return a random integer between a minimum and maximum value. Parameters: a. An integer. b. An integer. Here's how you can use it to get a random number between, 1 and 10:

letnumber=gu.randomInt(1,10);

randomFloat

Return a random floating point number between a minimum and maximum value. Parameters: a. Any number. b. Any number. Here's how you can use it to get a random floating point number between, 1 and 10:

letnumber=gu.randomFloat(1,10);

wait

Lets you wait for a specific number of milliseconds before running the next function.

wait(1000,runThisFunctionNext());

move

Move a sprite by adding it's velocity to it's position. The sprite must have vx and vy values for this to work. You can supply a single sprite, or a list of sprites, separated by commas.

gu.move(anySprite);

You can supply a single sprite, an argument list of sprite, or even a whole array containing sprites you want to move. But, make sure that all those sprites have vx and vy values that have been initialized to 0 somewhere in your code.

To make move work, update it each frame of your game loop, like this:

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positiongu.move(anySprite);}

worldCamera

A "camera" object that you can use to follow game objects around a large scrolling game world. The camera implements all the scrolling for you - you just need to give it a large game world to scroll. You can use any Pixi Container or Sprite as your game world.

Here's how to create a scrolling camera. Use the worldCamera method and supply these 4 things:

  1. The Pixi Container or Sprite you want to control.
  2. The width of the game world.
  3. The height of the game world.
  4. The HTML canvas object that represents your game screen. In Pixi, this is the PIXI.renderer.view object.
letcamera=gu.worldCamera(gameWorldContainer,worldWidth,worldHeight,canvas);

Be careful! The width and height values that the camera needs are probably different from the Container width and height that represents your game world. That's because Pixi containers and sprites dynamically change their size and width based on the positions of the child sprites they contain. If any of your game world's child sprites move around to the edges of the game world, they'll cause the width or height of the world to increase and make the camera scroll off the edges of the world. So, you'll probably need to supply some fixed values that define your worldWidth and worldHeight.

Use the camera's centerOver method to center the camera over a sprite.

camera.centerOver(anySprite);

You can use the camera's follow method to make the camera follow a sprite around the world.

camera.follow(anySprite);

Make sure call follow inside your game loop to see the effect!

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positioncamera.follow(anySprite);}

The camera will only start following the sprite when the sprite moves to within 25% of the screen edges, which is a very natural looking effect.

Line of sight

The lineOfSight method will return true if there’s clear line of sight between two sprites, and false if there isn’t. Here’s how to use it in your game code:

monster.lineOfSight = gu.lineOfSight(
monster, //Sprite one
alien, //Sprite two
boxes, //An array of obstacle sprites
16 //The distance between each collision point
);

The 4th argument determines the distance between collision points. For better performance, make this a large number, up to the maximum width of your smallest sprite (such as 64 or 32). For greater precision, use a smaller number. You can use the lineOfSight value to decide how to change certain things in your game. For example:

if (monster.lineOfSight) {
monster.show(monster.states.angry)
} else {
monster.show(monster.states.normal)
}

About

No description, website, or topics provided.

Resources

Stars

22 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Latest commit

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Game Utilities for Pixi (v3.9)

A library of helpful functions for making games and applications for the Pixi rendering engine.

(Important! This library targets Pixi v3.9, which is the most stable version of Pixi, and is the only version I can recommend using. This library will eventually be upgraded for Pixi v4 when the v4 branch matures.)

Setting up
distance: The pixel distance between two sprites
followEase: Ease a sprite to another sprite
followConstant: Follow a sprite at a constant rate
angle: Find the angle in radians between two sprites
rotateAroundSprite: Make a sprite rotate around another sprite
rotateAroundPoint: Make a point rotate around another point
randomInt: Get a random integer
randomFloat: Get a random floating point (decimal) number
wait: Set a delay before running the next task
move: Move a sprite by adding its velocity to its position
worldCamera: A camera for following objects around a large game world
lineOfSight: tells you whether a sprite is visible to another sprite

Setting up

Link to the gameUtilities.js file in your HTML document with a <script> tag, then create a new instance of the Game Utilities library in your JavaScript file like this:

letgu=newGameUtilities();

You can now access all the utility methods through the gu object.

Or, just copy and paster the function you need from the source file into your own project code.

distance

Find the distance in pixels between two sprites. Parameters: a. A sprite object. b. A sprite object. The function returns the number of pixels distance between the sprites.

letdistanceBetweenSprites=gu.distance(spriteA,spriteB);

If distance is calculated from the sprites' center points, assuming that the x/y anchor point is the sprite's top left corner. However, if a sprite's x/y anchor has been shifted, the distance will be calculated from that anchor point. All the Game Utility methods that depend on a distance calculation work in this same way (followEase, followConstant, rotateAroundSprite and angle).

followEase

Make a sprite ease to the position of another sprite. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The easing value, such as 0.3. A higher number makes the follower move faster.

gu.followEase(follower,leader,speed);

Use it inside a game loop.

followConstant

Make a sprite move towards another sprite at a constant speed. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The speed value, such as 3. The is the pixels per frame that the sprite will move. A higher number makes the follower move faster.

gu.followConstant(follower,leader,speed);

angle

Return the angle in Radians between two sprites. Parameters: a. A sprite object. b. A sprite object. You can use it to make a sprite rotate towards another sprite like this:

box.rotation=gu.angle(box,pointer);

rotateAroundSprite

Make a sprite rotate around another sprite. Parameters: a. The sprite you want to rotate. b. The sprite around which you want to rotate the first sprite. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundSprite(orbitingSprite,centerSprite,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

rotateAroundPoint

Make a point rotate around another point. Parameters: a. The point you want to rotate. b. The point around which you want to rotate the first point. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundPoint(orbitingPoint,centerPoint,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

randomInt

Return a random integer between a minimum and maximum value. Parameters: a. An integer. b. An integer. Here's how you can use it to get a random number between, 1 and 10:

letnumber=gu.randomInt(1,10);

randomFloat

Return a random floating point number between a minimum and maximum value. Parameters: a. Any number. b. Any number. Here's how you can use it to get a random floating point number between, 1 and 10:

letnumber=gu.randomFloat(1,10);

wait

Lets you wait for a specific number of milliseconds before running the next function.

wait(1000,runThisFunctionNext());

move

Move a sprite by adding it's velocity to it's position. The sprite must have vx and vy values for this to work. You can supply a single sprite, or a list of sprites, separated by commas.

gu.move(anySprite);

You can supply a single sprite, an argument list of sprite, or even a whole array containing sprites you want to move. But, make sure that all those sprites have vx and vy values that have been initialized to 0 somewhere in your code.

To make move work, update it each frame of your game loop, like this:

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positiongu.move(anySprite);}

worldCamera

A "camera" object that you can use to follow game objects around a large scrolling game world. The camera implements all the scrolling for you - you just need to give it a large game world to scroll. You can use any Pixi Container or Sprite as your game world.

Here's how to create a scrolling camera. Use the worldCamera method and supply these 4 things:

  1. The Pixi Container or Sprite you want to control.
  2. The width of the game world.
  3. The height of the game world.
  4. The HTML canvas object that represents your game screen. In Pixi, this is the PIXI.renderer.view object.
letcamera=gu.worldCamera(gameWorldContainer,worldWidth,worldHeight,canvas);

Be careful! The width and height values that the camera needs are probably different from the Container width and height that represents your game world. That's because Pixi containers and sprites dynamically change their size and width based on the positions of the child sprites they contain. If any of your game world's child sprites move around to the edges of the game world, they'll cause the width or height of the world to increase and make the camera scroll off the edges of the world. So, you'll probably need to supply some fixed values that define your worldWidth and worldHeight.

Use the camera's centerOver method to center the camera over a sprite.

camera.centerOver(anySprite);

You can use the camera's follow method to make the camera follow a sprite around the world.

camera.follow(anySprite);

Make sure call follow inside your game loop to see the effect!

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positioncamera.follow(anySprite);}

The camera will only start following the sprite when the sprite moves to within 25% of the screen edges, which is a very natural looking effect.

Line of sight

The lineOfSight method will return true if there’s clear line of sight between two sprites, and false if there isn’t. Here’s how to use it in your game code:

monster.lineOfSight = gu.lineOfSight(
monster, //Sprite one
alien, //Sprite two
boxes, //An array of obstacle sprites
16 //The distance between each collision point
);

The 4th argument determines the distance between collision points. For better performance, make this a large number, up to the maximum width of your smallest sprite (such as 64 or 32). For greater precision, use a smaller number. You can use the lineOfSight value to decide how to change certain things in your game. For example:

if (monster.lineOfSight) {
monster.show(monster.states.angry)
} else {
monster.show(monster.states.normal)
}

About

No description, website, or topics provided.

Resources

Stars

22 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Game Utilities for Pixi (v3.9)

A library of helpful functions for making games and applications for the Pixi rendering engine.

(Important! This library targets Pixi v3.9, which is the most stable version of Pixi, and is the only version I can recommend using. This library will eventually be upgraded for Pixi v4 when the v4 branch matures.)

Setting up
distance: The pixel distance between two sprites
followEase: Ease a sprite to another sprite
followConstant: Follow a sprite at a constant rate
angle: Find the angle in radians between two sprites
rotateAroundSprite: Make a sprite rotate around another sprite
rotateAroundPoint: Make a point rotate around another point
randomInt: Get a random integer
randomFloat: Get a random floating point (decimal) number
wait: Set a delay before running the next task
move: Move a sprite by adding its velocity to its position
worldCamera: A camera for following objects around a large game world
lineOfSight: tells you whether a sprite is visible to another sprite

Setting up

Link to the gameUtilities.js file in your HTML document with a <script> tag, then create a new instance of the Game Utilities library in your JavaScript file like this:

letgu=newGameUtilities();

You can now access all the utility methods through the gu object.

Or, just copy and paster the function you need from the source file into your own project code.

distance

Find the distance in pixels between two sprites. Parameters: a. A sprite object. b. A sprite object. The function returns the number of pixels distance between the sprites.

letdistanceBetweenSprites=gu.distance(spriteA,spriteB);

If distance is calculated from the sprites' center points, assuming that the x/y anchor point is the sprite's top left corner. However, if a sprite's x/y anchor has been shifted, the distance will be calculated from that anchor point. All the Game Utility methods that depend on a distance calculation work in this same way (followEase, followConstant, rotateAroundSprite and angle).

followEase

Make a sprite ease to the position of another sprite. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The easing value, such as 0.3. A higher number makes the follower move faster.

gu.followEase(follower,leader,speed);

Use it inside a game loop.

followConstant

Make a sprite move towards another sprite at a constant speed. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The speed value, such as 3. The is the pixels per frame that the sprite will move. A higher number makes the follower move faster.

gu.followConstant(follower,leader,speed);

angle

Return the angle in Radians between two sprites. Parameters: a. A sprite object. b. A sprite object. You can use it to make a sprite rotate towards another sprite like this:

box.rotation=gu.angle(box,pointer);

rotateAroundSprite

Make a sprite rotate around another sprite. Parameters: a. The sprite you want to rotate. b. The sprite around which you want to rotate the first sprite. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundSprite(orbitingSprite,centerSprite,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

rotateAroundPoint

Make a point rotate around another point. Parameters: a. The point you want to rotate. b. The point around which you want to rotate the first point. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundPoint(orbitingPoint,centerPoint,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

randomInt

Return a random integer between a minimum and maximum value. Parameters: a. An integer. b. An integer. Here's how you can use it to get a random number between, 1 and 10:

letnumber=gu.randomInt(1,10);

randomFloat

Return a random floating point number between a minimum and maximum value. Parameters: a. Any number. b. Any number. Here's how you can use it to get a random floating point number between, 1 and 10:

letnumber=gu.randomFloat(1,10);

wait

Lets you wait for a specific number of milliseconds before running the next function.

wait(1000,runThisFunctionNext());

move

Move a sprite by adding it's velocity to it's position. The sprite must have vx and vy values for this to work. You can supply a single sprite, or a list of sprites, separated by commas.

gu.move(anySprite);

You can supply a single sprite, an argument list of sprite, or even a whole array containing sprites you want to move. But, make sure that all those sprites have vx and vy values that have been initialized to 0 somewhere in your code.

To make move work, update it each frame of your game loop, like this:

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positiongu.move(anySprite);}

worldCamera

A "camera" object that you can use to follow game objects around a large scrolling game world. The camera implements all the scrolling for you - you just need to give it a large game world to scroll. You can use any Pixi Container or Sprite as your game world.

Here's how to create a scrolling camera. Use the worldCamera method and supply these 4 things:

  1. The Pixi Container or Sprite you want to control.
  2. The width of the game world.
  3. The height of the game world.
  4. The HTML canvas object that represents your game screen. In Pixi, this is the PIXI.renderer.view object.
letcamera=gu.worldCamera(gameWorldContainer,worldWidth,worldHeight,canvas);

Be careful! The width and height values that the camera needs are probably different from the Container width and height that represents your game world. That's because Pixi containers and sprites dynamically change their size and width based on the positions of the child sprites they contain. If any of your game world's child sprites move around to the edges of the game world, they'll cause the width or height of the world to increase and make the camera scroll off the edges of the world. So, you'll probably need to supply some fixed values that define your worldWidth and worldHeight.

Use the camera's centerOver method to center the camera over a sprite.

camera.centerOver(anySprite);

You can use the camera's follow method to make the camera follow a sprite around the world.

camera.follow(anySprite);

Make sure call follow inside your game loop to see the effect!

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positioncamera.follow(anySprite);}

The camera will only start following the sprite when the sprite moves to within 25% of the screen edges, which is a very natural looking effect.

Line of sight

The lineOfSight method will return true if there’s clear line of sight between two sprites, and false if there isn’t. Here’s how to use it in your game code:

monster.lineOfSight = gu.lineOfSight(
monster, //Sprite one
alien, //Sprite two
boxes, //An array of obstacle sprites
16 //The distance between each collision point
);

The 4th argument determines the distance between collision points. For better performance, make this a large number, up to the maximum width of your smallest sprite (such as 64 or 32). For greater precision, use a smaller number. You can use the lineOfSight value to decide how to change certain things in your game. For example:

if (monster.lineOfSight) {
monster.show(monster.states.angry)
} else {
monster.show(monster.states.normal)
}

About

No description, website, or topics provided.

Resources

Stars

22 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Game Utilities for Pixi (v3.9)

A library of helpful functions for making games and applications for the Pixi rendering engine.

(Important! This library targets Pixi v3.9, which is the most stable version of Pixi, and is the only version I can recommend using. This library will eventually be upgraded for Pixi v4 when the v4 branch matures.)

Setting up
distance: The pixel distance between two sprites
followEase: Ease a sprite to another sprite
followConstant: Follow a sprite at a constant rate
angle: Find the angle in radians between two sprites
rotateAroundSprite: Make a sprite rotate around another sprite
rotateAroundPoint: Make a point rotate around another point
randomInt: Get a random integer
randomFloat: Get a random floating point (decimal) number
wait: Set a delay before running the next task
move: Move a sprite by adding its velocity to its position
worldCamera: A camera for following objects around a large game world
lineOfSight: tells you whether a sprite is visible to another sprite

Setting up

Link to the gameUtilities.js file in your HTML document with a <script> tag, then create a new instance of the Game Utilities library in your JavaScript file like this:

letgu=newGameUtilities();

You can now access all the utility methods through the gu object.

Or, just copy and paster the function you need from the source file into your own project code.

distance

Find the distance in pixels between two sprites. Parameters: a. A sprite object. b. A sprite object. The function returns the number of pixels distance between the sprites.

letdistanceBetweenSprites=gu.distance(spriteA,spriteB);

If distance is calculated from the sprites' center points, assuming that the x/y anchor point is the sprite's top left corner. However, if a sprite's x/y anchor has been shifted, the distance will be calculated from that anchor point. All the Game Utility methods that depend on a distance calculation work in this same way (followEase, followConstant, rotateAroundSprite and angle).

followEase

Make a sprite ease to the position of another sprite. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The easing value, such as 0.3. A higher number makes the follower move faster.

gu.followEase(follower,leader,speed);

Use it inside a game loop.

followConstant

Make a sprite move towards another sprite at a constant speed. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The speed value, such as 3. The is the pixels per frame that the sprite will move. A higher number makes the follower move faster.

gu.followConstant(follower,leader,speed);

angle

Return the angle in Radians between two sprites. Parameters: a. A sprite object. b. A sprite object. You can use it to make a sprite rotate towards another sprite like this:

box.rotation=gu.angle(box,pointer);

rotateAroundSprite

Make a sprite rotate around another sprite. Parameters: a. The sprite you want to rotate. b. The sprite around which you want to rotate the first sprite. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundSprite(orbitingSprite,centerSprite,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

rotateAroundPoint

Make a point rotate around another point. Parameters: a. The point you want to rotate. b. The point around which you want to rotate the first point. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundPoint(orbitingPoint,centerPoint,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

randomInt

Return a random integer between a minimum and maximum value. Parameters: a. An integer. b. An integer. Here's how you can use it to get a random number between, 1 and 10:

letnumber=gu.randomInt(1,10);

randomFloat

Return a random floating point number between a minimum and maximum value. Parameters: a. Any number. b. Any number. Here's how you can use it to get a random floating point number between, 1 and 10:

letnumber=gu.randomFloat(1,10);

wait

Lets you wait for a specific number of milliseconds before running the next function.

wait(1000,runThisFunctionNext());

move

Move a sprite by adding it's velocity to it's position. The sprite must have vx and vy values for this to work. You can supply a single sprite, or a list of sprites, separated by commas.

gu.move(anySprite);

You can supply a single sprite, an argument list of sprite, or even a whole array containing sprites you want to move. But, make sure that all those sprites have vx and vy values that have been initialized to 0 somewhere in your code.

To make move work, update it each frame of your game loop, like this:

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positiongu.move(anySprite);}

worldCamera

A "camera" object that you can use to follow game objects around a large scrolling game world. The camera implements all the scrolling for you - you just need to give it a large game world to scroll. You can use any Pixi Container or Sprite as your game world.

Here's how to create a scrolling camera. Use the worldCamera method and supply these 4 things:

  1. The Pixi Container or Sprite you want to control.
  2. The width of the game world.
  3. The height of the game world.
  4. The HTML canvas object that represents your game screen. In Pixi, this is the PIXI.renderer.view object.
letcamera=gu.worldCamera(gameWorldContainer,worldWidth,worldHeight,canvas);

Be careful! The width and height values that the camera needs are probably different from the Container width and height that represents your game world. That's because Pixi containers and sprites dynamically change their size and width based on the positions of the child sprites they contain. If any of your game world's child sprites move around to the edges of the game world, they'll cause the width or height of the world to increase and make the camera scroll off the edges of the world. So, you'll probably need to supply some fixed values that define your worldWidth and worldHeight.

Use the camera's centerOver method to center the camera over a sprite.

camera.centerOver(anySprite);

You can use the camera's follow method to make the camera follow a sprite around the world.

camera.follow(anySprite);

Make sure call follow inside your game loop to see the effect!

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positioncamera.follow(anySprite);}

The camera will only start following the sprite when the sprite moves to within 25% of the screen edges, which is a very natural looking effect.

Line of sight

The lineOfSight method will return true if there’s clear line of sight between two sprites, and false if there isn’t. Here’s how to use it in your game code:

monster.lineOfSight = gu.lineOfSight(
monster, //Sprite one
alien, //Sprite two
boxes, //An array of obstacle sprites
16 //The distance between each collision point
);

The 4th argument determines the distance between collision points. For better performance, make this a large number, up to the maximum width of your smallest sprite (such as 64 or 32). For greater precision, use a smaller number. You can use the lineOfSight value to decide how to change certain things in your game. For example:

if (monster.lineOfSight) {
monster.show(monster.states.angry)
} else {
monster.show(monster.states.normal)
}

About

No description, website, or topics provided.

Resources

Stars

22 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Latest commit

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Game Utilities for Pixi (v3.9)

A library of helpful functions for making games and applications for the Pixi rendering engine.

(Important! This library targets Pixi v3.9, which is the most stable version of Pixi, and is the only version I can recommend using. This library will eventually be upgraded for Pixi v4 when the v4 branch matures.)

Setting up
distance: The pixel distance between two sprites
followEase: Ease a sprite to another sprite
followConstant: Follow a sprite at a constant rate
angle: Find the angle in radians between two sprites
rotateAroundSprite: Make a sprite rotate around another sprite
rotateAroundPoint: Make a point rotate around another point
randomInt: Get a random integer
randomFloat: Get a random floating point (decimal) number
wait: Set a delay before running the next task
move: Move a sprite by adding its velocity to its position
worldCamera: A camera for following objects around a large game world
lineOfSight: tells you whether a sprite is visible to another sprite

Setting up

Link to the gameUtilities.js file in your HTML document with a <script> tag, then create a new instance of the Game Utilities library in your JavaScript file like this:

letgu=newGameUtilities();

You can now access all the utility methods through the gu object.

Or, just copy and paster the function you need from the source file into your own project code.

distance

Find the distance in pixels between two sprites. Parameters: a. A sprite object. b. A sprite object. The function returns the number of pixels distance between the sprites.

letdistanceBetweenSprites=gu.distance(spriteA,spriteB);

If distance is calculated from the sprites' center points, assuming that the x/y anchor point is the sprite's top left corner. However, if a sprite's x/y anchor has been shifted, the distance will be calculated from that anchor point. All the Game Utility methods that depend on a distance calculation work in this same way (followEase, followConstant, rotateAroundSprite and angle).

followEase

Make a sprite ease to the position of another sprite. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The easing value, such as 0.3. A higher number makes the follower move faster.

gu.followEase(follower,leader,speed);

Use it inside a game loop.

followConstant

Make a sprite move towards another sprite at a constant speed. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The speed value, such as 3. The is the pixels per frame that the sprite will move. A higher number makes the follower move faster.

gu.followConstant(follower,leader,speed);

angle

Return the angle in Radians between two sprites. Parameters: a. A sprite object. b. A sprite object. You can use it to make a sprite rotate towards another sprite like this:

box.rotation=gu.angle(box,pointer);

rotateAroundSprite

Make a sprite rotate around another sprite. Parameters: a. The sprite you want to rotate. b. The sprite around which you want to rotate the first sprite. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundSprite(orbitingSprite,centerSprite,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

rotateAroundPoint

Make a point rotate around another point. Parameters: a. The point you want to rotate. b. The point around which you want to rotate the first point. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundPoint(orbitingPoint,centerPoint,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

randomInt

Return a random integer between a minimum and maximum value. Parameters: a. An integer. b. An integer. Here's how you can use it to get a random number between, 1 and 10:

letnumber=gu.randomInt(1,10);

randomFloat

Return a random floating point number between a minimum and maximum value. Parameters: a. Any number. b. Any number. Here's how you can use it to get a random floating point number between, 1 and 10:

letnumber=gu.randomFloat(1,10);

wait

Lets you wait for a specific number of milliseconds before running the next function.

wait(1000,runThisFunctionNext());

move

Move a sprite by adding it's velocity to it's position. The sprite must have vx and vy values for this to work. You can supply a single sprite, or a list of sprites, separated by commas.

gu.move(anySprite);

You can supply a single sprite, an argument list of sprite, or even a whole array containing sprites you want to move. But, make sure that all those sprites have vx and vy values that have been initialized to 0 somewhere in your code.

To make move work, update it each frame of your game loop, like this:

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positiongu.move(anySprite);}

worldCamera

A "camera" object that you can use to follow game objects around a large scrolling game world. The camera implements all the scrolling for you - you just need to give it a large game world to scroll. You can use any Pixi Container or Sprite as your game world.

Here's how to create a scrolling camera. Use the worldCamera method and supply these 4 things:

  1. The Pixi Container or Sprite you want to control.
  2. The width of the game world.
  3. The height of the game world.
  4. The HTML canvas object that represents your game screen. In Pixi, this is the PIXI.renderer.view object.
letcamera=gu.worldCamera(gameWorldContainer,worldWidth,worldHeight,canvas);

Be careful! The width and height values that the camera needs are probably different from the Container width and height that represents your game world. That's because Pixi containers and sprites dynamically change their size and width based on the positions of the child sprites they contain. If any of your game world's child sprites move around to the edges of the game world, they'll cause the width or height of the world to increase and make the camera scroll off the edges of the world. So, you'll probably need to supply some fixed values that define your worldWidth and worldHeight.

Use the camera's centerOver method to center the camera over a sprite.

camera.centerOver(anySprite);

You can use the camera's follow method to make the camera follow a sprite around the world.

camera.follow(anySprite);

Make sure call follow inside your game loop to see the effect!

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positioncamera.follow(anySprite);}

The camera will only start following the sprite when the sprite moves to within 25% of the screen edges, which is a very natural looking effect.

Line of sight

The lineOfSight method will return true if there’s clear line of sight between two sprites, and false if there isn’t. Here’s how to use it in your game code:

monster.lineOfSight = gu.lineOfSight(
monster, //Sprite one
alien, //Sprite two
boxes, //An array of obstacle sprites
16 //The distance between each collision point
);

The 4th argument determines the distance between collision points. For better performance, make this a large number, up to the maximum width of your smallest sprite (such as 64 or 32). For greater precision, use a smaller number. You can use the lineOfSight value to decide how to change certain things in your game. For example:

if (monster.lineOfSight) {
monster.show(monster.states.angry)
} else {
monster.show(monster.states.normal)
}

About

No description, website, or topics provided.

Resources

Stars

22 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Game Utilities for Pixi (v3.9)

A library of helpful functions for making games and applications for the Pixi rendering engine.

(Important! This library targets Pixi v3.9, which is the most stable version of Pixi, and is the only version I can recommend using. This library will eventually be upgraded for Pixi v4 when the v4 branch matures.)

Setting up
distance: The pixel distance between two sprites
followEase: Ease a sprite to another sprite
followConstant: Follow a sprite at a constant rate
angle: Find the angle in radians between two sprites
rotateAroundSprite: Make a sprite rotate around another sprite
rotateAroundPoint: Make a point rotate around another point
randomInt: Get a random integer
randomFloat: Get a random floating point (decimal) number
wait: Set a delay before running the next task
move: Move a sprite by adding its velocity to its position
worldCamera: A camera for following objects around a large game world
lineOfSight: tells you whether a sprite is visible to another sprite

Setting up

Link to the gameUtilities.js file in your HTML document with a <script> tag, then create a new instance of the Game Utilities library in your JavaScript file like this:

letgu=newGameUtilities();

You can now access all the utility methods through the gu object.

Or, just copy and paster the function you need from the source file into your own project code.

distance

Find the distance in pixels between two sprites. Parameters: a. A sprite object. b. A sprite object. The function returns the number of pixels distance between the sprites.

letdistanceBetweenSprites=gu.distance(spriteA,spriteB);

If distance is calculated from the sprites' center points, assuming that the x/y anchor point is the sprite's top left corner. However, if a sprite's x/y anchor has been shifted, the distance will be calculated from that anchor point. All the Game Utility methods that depend on a distance calculation work in this same way (followEase, followConstant, rotateAroundSprite and angle).

followEase

Make a sprite ease to the position of another sprite. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The easing value, such as 0.3. A higher number makes the follower move faster.

gu.followEase(follower,leader,speed);

Use it inside a game loop.

followConstant

Make a sprite move towards another sprite at a constant speed. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The speed value, such as 3. The is the pixels per frame that the sprite will move. A higher number makes the follower move faster.

gu.followConstant(follower,leader,speed);

angle

Return the angle in Radians between two sprites. Parameters: a. A sprite object. b. A sprite object. You can use it to make a sprite rotate towards another sprite like this:

box.rotation=gu.angle(box,pointer);

rotateAroundSprite

Make a sprite rotate around another sprite. Parameters: a. The sprite you want to rotate. b. The sprite around which you want to rotate the first sprite. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundSprite(orbitingSprite,centerSprite,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

rotateAroundPoint

Make a point rotate around another point. Parameters: a. The point you want to rotate. b. The point around which you want to rotate the first point. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundPoint(orbitingPoint,centerPoint,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

randomInt

Return a random integer between a minimum and maximum value. Parameters: a. An integer. b. An integer. Here's how you can use it to get a random number between, 1 and 10:

letnumber=gu.randomInt(1,10);

randomFloat

Return a random floating point number between a minimum and maximum value. Parameters: a. Any number. b. Any number. Here's how you can use it to get a random floating point number between, 1 and 10:

letnumber=gu.randomFloat(1,10);

wait

Lets you wait for a specific number of milliseconds before running the next function.

wait(1000,runThisFunctionNext());

move

Move a sprite by adding it's velocity to it's position. The sprite must have vx and vy values for this to work. You can supply a single sprite, or a list of sprites, separated by commas.

gu.move(anySprite);

You can supply a single sprite, an argument list of sprite, or even a whole array containing sprites you want to move. But, make sure that all those sprites have vx and vy values that have been initialized to 0 somewhere in your code.

To make move work, update it each frame of your game loop, like this:

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positiongu.move(anySprite);}

worldCamera

A "camera" object that you can use to follow game objects around a large scrolling game world. The camera implements all the scrolling for you - you just need to give it a large game world to scroll. You can use any Pixi Container or Sprite as your game world.

Here's how to create a scrolling camera. Use the worldCamera method and supply these 4 things:

  1. The Pixi Container or Sprite you want to control.
  2. The width of the game world.
  3. The height of the game world.
  4. The HTML canvas object that represents your game screen. In Pixi, this is the PIXI.renderer.view object.
letcamera=gu.worldCamera(gameWorldContainer,worldWidth,worldHeight,canvas);

Be careful! The width and height values that the camera needs are probably different from the Container width and height that represents your game world. That's because Pixi containers and sprites dynamically change their size and width based on the positions of the child sprites they contain. If any of your game world's child sprites move around to the edges of the game world, they'll cause the width or height of the world to increase and make the camera scroll off the edges of the world. So, you'll probably need to supply some fixed values that define your worldWidth and worldHeight.

Use the camera's centerOver method to center the camera over a sprite.

camera.centerOver(anySprite);

You can use the camera's follow method to make the camera follow a sprite around the world.

camera.follow(anySprite);

Make sure call follow inside your game loop to see the effect!

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positioncamera.follow(anySprite);}

The camera will only start following the sprite when the sprite moves to within 25% of the screen edges, which is a very natural looking effect.

Line of sight

The lineOfSight method will return true if there’s clear line of sight between two sprites, and false if there isn’t. Here’s how to use it in your game code:

monster.lineOfSight = gu.lineOfSight(
monster, //Sprite one
alien, //Sprite two
boxes, //An array of obstacle sprites
16 //The distance between each collision point
);

The 4th argument determines the distance between collision points. For better performance, make this a large number, up to the maximum width of your smallest sprite (such as 64 or 32). For greater precision, use a smaller number. You can use the lineOfSight value to decide how to change certain things in your game. For example:

if (monster.lineOfSight) {
monster.show(monster.states.angry)
} else {
monster.show(monster.states.normal)
}

About

No description, website, or topics provided.

Resources

Stars

22 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Game Utilities for Pixi (v3.9)

A library of helpful functions for making games and applications for the Pixi rendering engine.

(Important! This library targets Pixi v3.9, which is the most stable version of Pixi, and is the only version I can recommend using. This library will eventually be upgraded for Pixi v4 when the v4 branch matures.)

Setting up
distance: The pixel distance between two sprites
followEase: Ease a sprite to another sprite
followConstant: Follow a sprite at a constant rate
angle: Find the angle in radians between two sprites
rotateAroundSprite: Make a sprite rotate around another sprite
rotateAroundPoint: Make a point rotate around another point
randomInt: Get a random integer
randomFloat: Get a random floating point (decimal) number
wait: Set a delay before running the next task
move: Move a sprite by adding its velocity to its position
worldCamera: A camera for following objects around a large game world
lineOfSight: tells you whether a sprite is visible to another sprite

Setting up

Link to the gameUtilities.js file in your HTML document with a <script> tag, then create a new instance of the Game Utilities library in your JavaScript file like this:

letgu=newGameUtilities();

You can now access all the utility methods through the gu object.

Or, just copy and paster the function you need from the source file into your own project code.

distance

Find the distance in pixels between two sprites. Parameters: a. A sprite object. b. A sprite object. The function returns the number of pixels distance between the sprites.

letdistanceBetweenSprites=gu.distance(spriteA,spriteB);

If distance is calculated from the sprites' center points, assuming that the x/y anchor point is the sprite's top left corner. However, if a sprite's x/y anchor has been shifted, the distance will be calculated from that anchor point. All the Game Utility methods that depend on a distance calculation work in this same way (followEase, followConstant, rotateAroundSprite and angle).

followEase

Make a sprite ease to the position of another sprite. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The easing value, such as 0.3. A higher number makes the follower move faster.

gu.followEase(follower,leader,speed);

Use it inside a game loop.

followConstant

Make a sprite move towards another sprite at a constant speed. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The speed value, such as 3. The is the pixels per frame that the sprite will move. A higher number makes the follower move faster.

gu.followConstant(follower,leader,speed);

angle

Return the angle in Radians between two sprites. Parameters: a. A sprite object. b. A sprite object. You can use it to make a sprite rotate towards another sprite like this:

box.rotation=gu.angle(box,pointer);

rotateAroundSprite

Make a sprite rotate around another sprite. Parameters: a. The sprite you want to rotate. b. The sprite around which you want to rotate the first sprite. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundSprite(orbitingSprite,centerSprite,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

rotateAroundPoint

Make a point rotate around another point. Parameters: a. The point you want to rotate. b. The point around which you want to rotate the first point. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundPoint(orbitingPoint,centerPoint,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

randomInt

Return a random integer between a minimum and maximum value. Parameters: a. An integer. b. An integer. Here's how you can use it to get a random number between, 1 and 10:

letnumber=gu.randomInt(1,10);

randomFloat

Return a random floating point number between a minimum and maximum value. Parameters: a. Any number. b. Any number. Here's how you can use it to get a random floating point number between, 1 and 10:

letnumber=gu.randomFloat(1,10);

wait

Lets you wait for a specific number of milliseconds before running the next function.

wait(1000,runThisFunctionNext());

move

Move a sprite by adding it's velocity to it's position. The sprite must have vx and vy values for this to work. You can supply a single sprite, or a list of sprites, separated by commas.

gu.move(anySprite);

You can supply a single sprite, an argument list of sprite, or even a whole array containing sprites you want to move. But, make sure that all those sprites have vx and vy values that have been initialized to 0 somewhere in your code.

To make move work, update it each frame of your game loop, like this:

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positiongu.move(anySprite);}

worldCamera

A "camera" object that you can use to follow game objects around a large scrolling game world. The camera implements all the scrolling for you - you just need to give it a large game world to scroll. You can use any Pixi Container or Sprite as your game world.

Here's how to create a scrolling camera. Use the worldCamera method and supply these 4 things:

  1. The Pixi Container or Sprite you want to control.
  2. The width of the game world.
  3. The height of the game world.
  4. The HTML canvas object that represents your game screen. In Pixi, this is the PIXI.renderer.view object.
letcamera=gu.worldCamera(gameWorldContainer,worldWidth,worldHeight,canvas);

Be careful! The width and height values that the camera needs are probably different from the Container width and height that represents your game world. That's because Pixi containers and sprites dynamically change their size and width based on the positions of the child sprites they contain. If any of your game world's child sprites move around to the edges of the game world, they'll cause the width or height of the world to increase and make the camera scroll off the edges of the world. So, you'll probably need to supply some fixed values that define your worldWidth and worldHeight.

Use the camera's centerOver method to center the camera over a sprite.

camera.centerOver(anySprite);

You can use the camera's follow method to make the camera follow a sprite around the world.

camera.follow(anySprite);

Make sure call follow inside your game loop to see the effect!

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positioncamera.follow(anySprite);}

The camera will only start following the sprite when the sprite moves to within 25% of the screen edges, which is a very natural looking effect.

Line of sight

The lineOfSight method will return true if there’s clear line of sight between two sprites, and false if there isn’t. Here’s how to use it in your game code:

monster.lineOfSight = gu.lineOfSight(
monster, //Sprite one
alien, //Sprite two
boxes, //An array of obstacle sprites
16 //The distance between each collision point
);

The 4th argument determines the distance between collision points. For better performance, make this a large number, up to the maximum width of your smallest sprite (such as 64 or 32). For greater precision, use a smaller number. You can use the lineOfSight value to decide how to change certain things in your game. For example:

if (monster.lineOfSight) {
monster.show(monster.states.angry)
} else {
monster.show(monster.states.normal)
}

About

No description, website, or topics provided.

Resources

Stars

22 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Latest commit

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Game Utilities for Pixi (v3.9)

A library of helpful functions for making games and applications for the Pixi rendering engine.

(Important! This library targets Pixi v3.9, which is the most stable version of Pixi, and is the only version I can recommend using. This library will eventually be upgraded for Pixi v4 when the v4 branch matures.)

Setting up
distance: The pixel distance between two sprites
followEase: Ease a sprite to another sprite
followConstant: Follow a sprite at a constant rate
angle: Find the angle in radians between two sprites
rotateAroundSprite: Make a sprite rotate around another sprite
rotateAroundPoint: Make a point rotate around another point
randomInt: Get a random integer
randomFloat: Get a random floating point (decimal) number
wait: Set a delay before running the next task
move: Move a sprite by adding its velocity to its position
worldCamera: A camera for following objects around a large game world
lineOfSight: tells you whether a sprite is visible to another sprite

Setting up

Link to the gameUtilities.js file in your HTML document with a <script> tag, then create a new instance of the Game Utilities library in your JavaScript file like this:

letgu=newGameUtilities();

You can now access all the utility methods through the gu object.

Or, just copy and paster the function you need from the source file into your own project code.

distance

Find the distance in pixels between two sprites. Parameters: a. A sprite object. b. A sprite object. The function returns the number of pixels distance between the sprites.

letdistanceBetweenSprites=gu.distance(spriteA,spriteB);

If distance is calculated from the sprites' center points, assuming that the x/y anchor point is the sprite's top left corner. However, if a sprite's x/y anchor has been shifted, the distance will be calculated from that anchor point. All the Game Utility methods that depend on a distance calculation work in this same way (followEase, followConstant, rotateAroundSprite and angle).

followEase

Make a sprite ease to the position of another sprite. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The easing value, such as 0.3. A higher number makes the follower move faster.

gu.followEase(follower,leader,speed);

Use it inside a game loop.

followConstant

Make a sprite move towards another sprite at a constant speed. Parameters: a. A sprite object. This is the follower sprite. b. A sprite object. This is the leader sprite that the follower will chase. c. The speed value, such as 3. The is the pixels per frame that the sprite will move. A higher number makes the follower move faster.

gu.followConstant(follower,leader,speed);

angle

Return the angle in Radians between two sprites. Parameters: a. A sprite object. b. A sprite object. You can use it to make a sprite rotate towards another sprite like this:

box.rotation=gu.angle(box,pointer);

rotateAroundSprite

Make a sprite rotate around another sprite. Parameters: a. The sprite you want to rotate. b. The sprite around which you want to rotate the first sprite. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundSprite(orbitingSprite,centerSprite,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

rotateAroundPoint

Make a point rotate around another point. Parameters: a. The point you want to rotate. b. The point around which you want to rotate the first point. c. The distance, in pixels, that the rotating sprite should be offset from the center. d. The angle of rotations, in radians.

gu.rotateAroundPoint(orbitingPoint,centerPoint,50,angleInRadians);

Use it inside a game loop, and make sure you update the angle value (the 4th argument) each frame.

randomInt

Return a random integer between a minimum and maximum value. Parameters: a. An integer. b. An integer. Here's how you can use it to get a random number between, 1 and 10:

letnumber=gu.randomInt(1,10);

randomFloat

Return a random floating point number between a minimum and maximum value. Parameters: a. Any number. b. Any number. Here's how you can use it to get a random floating point number between, 1 and 10:

letnumber=gu.randomFloat(1,10);

wait

Lets you wait for a specific number of milliseconds before running the next function.

wait(1000,runThisFunctionNext());

move

Move a sprite by adding it's velocity to it's position. The sprite must have vx and vy values for this to work. You can supply a single sprite, or a list of sprites, separated by commas.

gu.move(anySprite);

You can supply a single sprite, an argument list of sprite, or even a whole array containing sprites you want to move. But, make sure that all those sprites have vx and vy values that have been initialized to 0 somewhere in your code.

To make move work, update it each frame of your game loop, like this:

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positiongu.move(anySprite);}

worldCamera

A "camera" object that you can use to follow game objects around a large scrolling game world. The camera implements all the scrolling for you - you just need to give it a large game world to scroll. You can use any Pixi Container or Sprite as your game world.

Here's how to create a scrolling camera. Use the worldCamera method and supply these 4 things:

  1. The Pixi Container or Sprite you want to control.
  2. The width of the game world.
  3. The height of the game world.
  4. The HTML canvas object that represents your game screen. In Pixi, this is the PIXI.renderer.view object.
letcamera=gu.worldCamera(gameWorldContainer,worldWidth,worldHeight,canvas);

Be careful! The width and height values that the camera needs are probably different from the Container width and height that represents your game world. That's because Pixi containers and sprites dynamically change their size and width based on the positions of the child sprites they contain. If any of your game world's child sprites move around to the edges of the game world, they'll cause the width or height of the world to increase and make the camera scroll off the edges of the world. So, you'll probably need to supply some fixed values that define your worldWidth and worldHeight.

Use the camera's centerOver method to center the camera over a sprite.

camera.centerOver(anySprite);

You can use the camera's follow method to make the camera follow a sprite around the world.

camera.follow(anySprite);

Make sure call follow inside your game loop to see the effect!

functiongameLoop(){requestAnimationFrame(gameLoop);//Call `camera.follow` each frame to update the camera's positioncamera.follow(anySprite);}

The camera will only start following the sprite when the sprite moves to within 25% of the screen edges, which is a very natural looking effect.

Line of sight

The lineOfSight method will return true if there’s clear line of sight between two sprites, and false if there isn’t. Here’s how to use it in your game code:

monster.lineOfSight = gu.lineOfSight(
monster, //Sprite one
alien, //Sprite two
boxes, //An array of obstacle sprites
16 //The distance between each collision point
);

The 4th argument determines the distance between collision points. For better performance, make this a large number, up to the maximum width of your smallest sprite (such as 64 or 32). For greater precision, use a smaller number. You can use the lineOfSight value to decide how to change certain things in your game. For example:

if (monster.lineOfSight) {
monster.show(monster.states.angry)
} else {
monster.show(monster.states.normal)
}

About

No description, website, or topics provided.

Resources

Stars

22 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages