Last active
August 10, 2021 14:44
-
-
Save jessefreeman/4712694 to your computer and use it in GitHub Desktop.
Here is a simple radar plugin based on https://github.com/bitmapshades/Minimap. This is still hard coded to my own game and would need some modification in order to run in another ImpactJS project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ig.module( | |
'plugins.radar' | |
) | |
.requires( | |
'impact.impact', | |
'impact.entity', | |
'game.entities.base-alien', | |
'game.entities.air-powerup', | |
'game.entities.energy-powerup', | |
'game.entities.artifact', | |
'game.entities.crystal', | |
'game.entities.exit-sign', | |
'game.entities.health-powerup' | |
) | |
.defines(function () { | |
Radar = ig.Class.extend({ | |
// init objects | |
minimapImg: new ig.Image('media/sprites/radar-sprite.png'), | |
markerImg: new ig.Image('media/sprites/radar-marker-sprites.png'), | |
markerSize: { x: 8, y: 8, xOffset: 4, yOffset: 4 }, | |
updateTimer: new ig.Timer(), | |
player: null, | |
init: function (x, y) { | |
// defaults | |
this.x = x; | |
this.y = y; | |
this.width = this.minimapImg.width; | |
this.height = this.minimapImg.height; | |
this.displayRange = this.minimapImg.width * .5; | |
this.range = this.displayRange + 50; | |
this.scalefactor = 5; | |
this.offsetx = this.width * .5; | |
this.offsety = this.height * .5; | |
// Minimap centre point | |
this.mapcenter = { x: this.x + this.offsetx, y: this.y + this.offsety }; | |
}, | |
draw: function () { | |
this.minimapImg.draw(this.x, this.y); | |
this.renderEntities(); | |
}, | |
renderEntities: function () { | |
if (ig.game.player) { | |
var entities = ig.game.entities; | |
var total = entities.length; | |
var entity; | |
for (var i = 0; i < total; i++) { | |
entity = entities[i]; | |
var point = this.calculateNewPosition(ig.game.player, entity); | |
point = this.limit(this.mapcenter.x, this.mapcenter.y, point.x, point.y, this.range) | |
if (point.dist < this.range || entity.radarLock == true) { | |
if (point.dist > this.displayRange) { | |
point = this.limitToCircle(point.dx, point.dy, point.x1, point.y1, this.displayRange, point.dist); | |
} | |
this.markerImg.drawTile(point.x - this.markerSize.xOffset, point.y - this.markerSize.yOffset, entity.radarSprite, this.markerSize.x, this.markerSize.y); | |
entity.isInView(true); | |
} else { | |
entity.isInView(false); | |
} | |
} | |
} | |
}, | |
calculateNewPosition: function (player, entity) { | |
var xv = Math.abs(player.pos.x - entity.pos.x) / this.scalefactor; | |
var yv = Math.abs(player.pos.y - entity.pos.y) / this.scalefactor; | |
//Get relative entity position | |
if (player.pos.x > entity.pos.x) { | |
dx = -1; | |
} | |
else if (player.pos.x < entity.pos.x) { | |
dx = 1; | |
} | |
if (player.pos.y > entity.pos.y) { | |
dy = -1; | |
} | |
else if (player.pos.y < entity.pos.y) { | |
dy = 1; | |
} | |
// Draw entity marker relative to centre point | |
return { x: this.mapcenter.x + (dx * xv), y: this.mapcenter.y + (dy * yv) }; | |
}, | |
limit: function (x1, y1, x2, y2, radius) { | |
// the vector between the two points | |
var dx = x2 - x1, | |
dy = y2 - y1, | |
distanceSquared = (dx * dx) + (dy * dy); | |
return { x: x2, y: y2, dist: Math.sqrt(distanceSquared), dx: dx, dy: dy, x1: x1, y1: y1 }; | |
}, | |
limitToCircle: function (dx, dy, x1, y1, radius, distance) { | |
//console.log("limit circle"); | |
ratio = radius / distance; | |
return { | |
x: (dx * ratio) + x1, | |
y: (dy * ratio) + y1, | |
dist: radius | |
}; | |
} | |
}); | |
ig.Entity.inject({ | |
inView: false, | |
isInView: function(value) { | |
this.inView = value; | |
} | |
}); | |
EntityBaseAlien.inject({ | |
radarSprite: 0 | |
}); | |
EntityAirPowerup.inject({ | |
radarSprite: 1 | |
}); | |
EntityEnergyPowerup.inject({ | |
radarSprite: 2 | |
}); | |
EntityHealthPowerup.inject({ | |
radarSprite: 3 | |
}); | |
EntityArtifact.inject({ | |
radarSprite: 4 | |
}); | |
EntityCrystal.inject({ | |
radarSprite: 5 | |
}); | |
EntityExitSign.inject({ | |
radarSprite: 6, | |
radarLock: true | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment