Skip to content

Instantly share code, notes, and snippets.

@dkozar
Created January 16, 2014 23:12
Show Gist options
  • Save dkozar/8465313 to your computer and use it in GitHub Desktop.
Save dkozar/8465313 to your computer and use it in GitHub Desktop.
Push-flag demo
/*
Copyright (c) 2003-2004 Danko Kozar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
numberOfObjects = 0;
level = 0;
numberOfLives = 99;
roomx = 800;
roomy = 600;
linkedList = Array();
// --- drawing --------------------------------
stil = function (a, b, c) {
// stil a,b,c: širina linije, boja, alpha
lineStyle(a, b, c);
};
plot = function (x, y) {
// koordinate
moveTo(x, y);
};
draw = function (x, y) {
// koordinate
lineTo(x, y);
};
stil(2, 0x777777, 100);
plot(0, 0);
draw(roomx, 0);
draw(roomx, roomy);
draw(0, roomy);
draw(0, 0);
// ------------------------------------
function hitTest2(obj1, obj2) {
if ((Math.abs((obj1.x+obj1.a/2)-(obj2.x+obj2.a/2))<(obj1.a+obj2.a)/2) and (Math.abs((obj1.y+obj1.b/2)-(obj2.y+obj2.b/2))<(obj1.b+obj2.b)/2)) {
//trace("Hit");
return (true);
}
}
function WallTest(obj) {
if ((Math.abs((obj.x+obj.a/2)-roomx)<(obj.a/2)) or (Math.abs((obj.x+obj.a/2))<(obj.a/2)) or (Math.abs((obj.y+obj.b/2)-roomy)<(obj.b/2)) or (Math.abs((obj.y+obj.b/2))<(obj.b/2))) {
//trace("WallTest");
return (true);
}
}
function Die() {
numberOfLives--;
if (numberOfLives<=0) {
numberOfLives = 99;
}
}
function Control(object) {
if (Key.isDown(Key.LEFT)) {
object.push |= 2;
} else if (Key.isDown(Key.RIGHT)) {
object.push |= 8;
} else if (Key.isDown(Key.UP)) {
object.push |= 4;
} else if (Key.isDown(Key.DOWN)) {
object.push |= 1;
}
}
function object(instance, clip, x, y, a, b, move, turn, poss, slide, push, lthl, speed) {
this.x = x;
this.y = y;
this.a = a;
this.b = b;
this.clip = clip;
this.instance = instance;
this.push = push | move;
this.poss = poss;
this.slide = slide;
this.turn = turn;
this.move = move;
this.lthl = lthl;
this.speed = speed;
this.init = function() {
linkedList.push(this);
_root.level++;
attachMovie(this.clip, this.instance, _root.level);
setProperty(this.instance, _x, this.x);
setProperty(this.instance, _y, this.y);
numberOfObjects++;
};
this.placeIt = function() {
setProperty(this.instance, _x, this.x);
setProperty(this.instance, _y, this.y);
};
this.pushIt = function(flag) {
this.push = flag;
};
this.pushed = function() {
if ((this.push) != 0) {
//trace("pushed "+this);
return (true);
} else {
return (false);
}
};
this.zeroOrSlide = function() {
//trace("Micem "+object);
this.placeIt();
// ako predmet moze slideati, push flag mu ostaje setiran (ako je slide flag nula, onda postaje nula)
this.push &= this.slide;
};
this.turnOn = function() {
switch (this.turn) {
case 1 :
// if move-flag is 1, it becomes 16, so it shifts to the right
if (this.move == 1) {
this.move = 16;
}
// 1 bit to the right
this.move >>= 1;
break;
case 2 :
// if move-flag is 16, it becomes 1, so it shifts to the left
// 1 bit to the left
this.move <<= 1;
if (this.move == 16) {
this.move = 1;
}
break;
}
};
this.say = function(text) {
_root.level++;
eval(this.instance).swapDepths(_root.level);
eval(this.instance).cloud.text = text.toUpperCase();
eval(this.instance).cloud.gotoAndPlay("start");
};
this.moveObject = function() {
// povecava koordinate ovisno o push-flagu
this.x += ((((this.push & this.poss & 8) >> 3)-((this.push & this.poss & 2) >> 1)))*this.speed;
this.y += (((this.push & this.poss & 1)-((this.push & this.poss & 4) >> 2)))*this.speed;
// testiranje hitTesta sa zidom
if (WallTest(this)) {
// ako je WallTest
// vraca koordinate naseg objecta natrag, jer se ne moze pomaknuti
this.x -= ((((this.push & 8) >> 3)-((this.push & 2) >> 1)))*this.speed;
this.y -= (((this.push & 1)-((this.push & 4) >> 2)))*this.speed;
// ponistava push flag naseg objecta
this.push = 0;
// skretanje - ukoliko object ima setiran move flag
if (this.move != 0) {
this.turnOn();
}
}
// testiranje medjusobnog hitTest2a
// prelazi listu objekata
for (i=0; i<numberOfObjects; i++) {
that = linkedList[i];;
// a ako to nije taj isti object...
if (that != this) {
// provjerava hitTest2 s njim
if (hitTest2(this, that)) {
that.say("I WAS HIT\nBY "+this.instance);
this.say("I BUMPED\nINTO "+that.instance);
// ako je object je smrtonosan, a hitTest2 je s likom ---> lik je mrtav
if (that == object1) {
if (this.lthl) {
Die();
}
}
// ako je object lik, a hitTest2 je sa smrtonosnim objectom ---> lik je mrtav
if (this == object1) {
if (that.lthl) {
Die();
}
}
// ako je hitTest2 - prenosi svoj impuls na taj object (setira push flag)
that.push = this.push;
// vraca koordinate naseg objecta natrag, jer se ne moze pomaknuti
this.x -= ((((this.push & 8) >> 3)-((this.push & 2) >> 1)))*this.speed;
this.y -= (((this.push & 1)-((this.push & 4) >> 2)))*this.speed;
// ponistava push flag naseg objecta
this.push = 0;
// skretanje - ukoliko object ima setiran move flag
if (this.move != 0) {
this.turnOn();
}
}
}
}
this.placeIt();
// ako predmet moze slideati, push flag mu ostaje setiran (ako je slide flag nula, onda postaje nula)
this.push &= this.slide;
//trace("Može se pomaknuti object "+i);
return (true);
};
}
// --- initialization ----------------------------
// main character
object1 = new object("player", "MC1", 100, 100, 100, 100, 0, 0, 15, 0, 0, 0, 5);
object1.init();
object2 = new object("object1", "MC2", 500, 100, 50, 200, 0, 0, 15, 0, 0, 0, 5);
object2.init();
object3 = new object("object2", "MC3", 300, 250, 150, 50, 0, 0, 10, 10, 0, 0, 5);
object3.init();
object4 = new object("object3", "MC4", 300, 350, 100, 50, 0, 0, 5, 0, 0, 0, 5);
object4.init();
object5 = new object("object4", "MC5", 150, 250, 100, 200, 0, 0, 15, 0, 0, 0, 5);
object5.init();
object10 = new object("object5", "MC7", 500, 350, 100, 100, 0, 0, 15, 0, 0, 0, 5);
object10.init();
// enemies
object6 = new object("enemy1", "MC6", 400, 50, 100, 100, 2, 1, 15, 0, 0, 1, 3);
object6.init();
object7 = new object("enemy2", "MC6", 600, 350, 100, 100, 2, 2, 15, 0, 0, 1, 3);
object7.init();
object8 = new object("enemy3", "MC6", 550, 500, 100, 100, 1, 1, 15, 0, 0, 1, 5);
object8.init();
object9 = new object("enemy4", "MC6", 300, 450, 100, 100, 8, 2, 15, 0, 0, 1, 2);
object9.init();
// --- main program loop ----------------------------
this.onEnterFrame = function() {
Control(object1);
for (j=1; j<=numberOfObjects; j++) {
// za svaki object OR-amo push i move flag
with (eval("object"+j)) {
push |= move;
// ako je za taj object push flag aktiviran
if (pushed()) {
// i ako se moze pomaknuti u tom smjeru
moveObject();
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment