Skip to content

Instantly share code, notes, and snippets.

@PifyZ
Last active May 19, 2016 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PifyZ/285f503cd7cce9bea453884d90e5fa1e to your computer and use it in GitHub Desktop.
Save PifyZ/285f503cd7cce9bea453884d90e5fa1e to your computer and use it in GitHub Desktop.
###
const map = [
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ],
[ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
]
###
const map = [
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
]
namespace Game {
const GRID = 20
var hero Entity
}
namespace Entity {
var ALL = List<Entity>.new
}
class Point {
var x double
var y double
def clone Point {
return Point.new(x, y)
}
}
class Entity {
var position Point
var velocity Point
var width double
var height double
var friction double
var mass double
def new(x double, y double, width double, height double) {
self.width = width
self.height = height
self.position = Point.new(x, y)
self.velocity = Point.new(0, 0)
friction = 0
mass = 1
}
def collision(r Entity) bool {
return position.x < r.position.x + r.width &&
position.x + width > r.position.x &&
position.y < r.position.y + r.height &&
position.y + height > r.position.y
}
def update {
for e in ALL {
if e != self {
if collision(e) {
###
var px = velocity.x * mass + e.velocity.x * e.mass
var py = velocity.y * mass + e.velocity.y * e.mass
var qtyMvt = Point.new(0, 0)
qtyMvt.x = px * (mass / (mass + e.mass))
qtyMvt.y = py * (mass / (mass + e.mass))
velocity.x = qtyMvt.x / mass
velocity.y = qtyMvt.y / mass
###
e.velocity.x /= friction * (mass + e.mass)
e.velocity.y /= friction * (mass + e.mass)
velocity = e.velocity.clone
}
}
}
position.x += velocity.x
position.y += velocity.y
###
var collision = false
for y in 0 .. map.count - 1 {
for x in 0 .. map[y].count - 1 {
var tile = map[y][x]
if tile == 1 {
var xOverlaps = position.x < (x + 1) * Game.GRID &&
position.x + width > x * Game.GRID
var yOverlaps = position.y < (y + 1) * Game.GRID &&
position.y + height > y * Game.GRID
collision = xOverlaps && yOverlaps
if collision {
console.log("AHH")
}
}
}
}
###
}
def getCol(x int, y int) bool {
return map[y][x] == 1
}
def draw(ctx CanvasRenderingContext2D) {
ctx.fillStyle = "rgb(128, 128, 0)"
ctx.fillRect(position.x as int, position.y as int, width, height)
}
}
class Player : Entity {
def new(x double, y double) {
super(x, y, 20, 20)
friction = 1
mass = 20
}
over draw(ctx CanvasRenderingContext2D) {
ctx.fillStyle = "rgb(0, 0, 255)"
ctx.fillRect(position.x as int, position.y as int, width, height)
}
}
class Crate : Entity {
def new(x double, y double) {
super(x, y, 20, 20)
friction = 0
mass = 10
}
over draw(ctx CanvasRenderingContext2D) {
ctx.fillStyle = "rgb(128, 142, 0)"
ctx.fillRect(position.x as int, position.y as int, width, height)
}
}
# APPLICATION
# -----------
var ctx CanvasRenderingContext2D
var kb = input.Keyboard.new
def tick {
Entity.ALL[0].velocity.x = 0
Entity.ALL[0].velocity.y = 0
if kb.down(Key.Left) {
Entity.ALL[0].velocity.x -= 3
}
if kb.down(Key.Right) {
Entity.ALL[0].velocity.x += 2
}
if kb.down(Key.Up) {
Entity.ALL[0].velocity.y -= 2
}
if kb.down(Key.Down) {
Entity.ALL[0].velocity.y += 2
}
ctx.clearRect(0, 0, 640, 480)
###
ctx.fillStyle = "rgb(0, 0, 0)"
for y in 0 .. map.count - 1 {
for x in 0 .. map[y].count - 1 {
if map[y][x] == 1 {
ctx.fillRect(x * Game.GRID, y * Game.GRID, Game.GRID, Game.GRID)
}
}
}
###
for entity in Entity.ALL {
entity.update
}
for entity in Entity.ALL {
entity.draw(ctx)
}
requestAnimationFrame(=> tick)
}
@entry
def main {
var images = {
"player": "player.png",
"wall": "wall.png",
"ground": "ground.png",
"font": "font.png",
"font2": "font2.png",
"barrel": "block-barrel.png",
"mine": "block-mine.png",
"dynamite": "block-dynamite.png",
"crate": "block-crate.png",
"bullet": "bullet-normal.png",
"rocketLauncherBullet": "bullet-rocket-launcher.png",
"grenade": "bullet-grenade.png",
"miniatures": "miniatures-weapons.png",
"bonusIncreaseHealth": "bonus-increase-health.png"
}
Img.setPath("assets/img")
Img.setCallback(=> run)
Img.load(images)
}
def run {
var canvas = document.getElementById("app") as HTMLCanvasElement
canvas.width = 640
canvas.height = 480
ctx = canvas.getContext2D
ctx.scale(2, 2)
Entity.ALL.append(Player.new(20, 20))
Entity.ALL.append(Crate.new(150, 150))
Entity.ALL.append(Crate.new(180, 220))
Game.hero = Entity.ALL[0]
tick
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment