metanohi-misc-subsites/subsites/projects/eonaton/game/stuff/backup.20-12-08-23.12/functions.js

260 lines
7.5 KiB
JavaScript

function style_bg_img(background) {
var cssurl
if (background.indexOf(".")!=-1) cssurl=["url(",")"]
else cssurl=["",""]
return cssurl[0]+background+cssurl[1]
}
function create(scr) {
scr.create()
link(scr)
}
function remove(obj) {
if (obj.type="character") take_life(obj)
obj.remove()
delete obj
}
function getkeycode(e) {
var keycode
if (window.event) keycode=window.event.keyCode
else if (e) keycode=e.which
else keycode=0
return keycode
}
function START_systems() {
key_up=false
key_right=false
key_down=false
key_left=false
key_shift=false
key_control=false
key_order=new Array()
for (var i=0;i<4;i++) {
key_order[i]=0
}
direction=7
character_state=1
document.onkeydown=keydown
document.onkeyup=keyup
characters=new Array()
row_num=new Array()
col_num=new Array()
col_length=new Array()
define_pixel_movement(3,5)
frame_showing_speed=150
character_movement_speed=50
frame_showing()
character_movement()
//setInterval(debugging,100)
}
function debugging() {
document.title=direction+" | "+col_length[0]+" | "+col_num[0]+" | "+character_state
}
function define_pixel_movement(walk,run) {
pixel_movement=[0,walk,run]
diagonal_pixel_movement=[0,Math.sqrt(walk*walk/2),Math.sqrt(run*run/2)]
}
function show_frame(cha_num,row,col) {
characters[cha_num].elem.style.backgroundPosition="-"+(col*(cha.width+1)+1)+"px -"+(cha.dirs[row][0]*(cha.height+1)+1)+"px"
}
function frame_showing() {
for (var i=0;i<characters.length;i++) {
if (col_num[i]<col_length[i]) col_num[i]++
else col_num[i]=0
show_frame(i,row_num[0],characters[i].dirs[row_num[i]][character_state][col_num[i]])
}
setTimeout(frame_showing,frame_showing_speed)
}
function character_movement() {
var pixmove=pixel_movement[character_state-1]
var dia_pixmove=diagonal_pixel_movement[character_state-1]
var x,y
for (var i=0;i<characters.length;i++) {
switch(direction) {
case 1:
y=characters[i].posY-pixmove
if (y>-1) characters[i].posY=y
break;
case 2:
y=characters[i].posY-dia_pixmove
x=characters[i].posX+dia_pixmove
if (y>-1) characters[i].posY=y
if (x<current_screen.width-characters[i].width+1) characters[i].posX=x
break;
case 3:
x=characters[i].posX+pixmove
if (x<current_screen.width-characters[i].width+1) characters[i].posX=x
break;
case 4:
x=characters[i].posX+dia_pixmove
y=characters[i].posY+dia_pixmove
if (x<current_screen.width-characters[i].width+1) characters[i].posX=x
if (y<current_screen.height-characters[i].height+1) characters[i].posY=y
break;
case 5:
y=characters[i].posY+pixmove
if (y<current_screen.height-characters[i].height+1) characters[i].posY=y
break;
case 6:
y=characters[i].posY+dia_pixmove
x=characters[i].posX-dia_pixmove
if (y<current_screen.height-characters[i].height+1) characters[i].posY=y
if (x>-1) characters[i].posX=x
break;
case 7:
x=characters[i].posX-pixmove
if (x>-1) characters[i].posX=x
break;
case 8:
x=characters[i].posX-dia_pixmove
y=characters[i].posY-dia_pixmove
if (x>-1) characters[i].posX=x
if (y>-1) characters[i].posY=y
break;
}
characters[i].elem.style.left=characters[i].posX+"px"
characters[i].elem.style.top=characters[i].posY+"px"
}
setTimeout(character_movement,character_movement_speed)
}
function get_character_state() {
if (key_order[0]==0) character_state=1
else if (!key_shift && key_order[0]>0) character_state=2
else if (key_shift && key_order[0]>0) character_state=3
}
function get_row_num() {
for (var i=0;i<characters.length;i++) {
if (!characters[i].dirs[direction-1]) {
if (direction==2 && key_order[1]==1) row_num[i]=0
else if (direction==2 && key_order[1]==2) row_num[i]=2
else if (direction==4 && key_order[1]==2) row_num[i]=2
else if (direction==4 && key_order[1]==3) row_num[i]=4
else if (direction==6 && key_order[1]==3) row_num[i]=4
else if (direction==6 && key_order[1]==4) row_num[i]=6
else if (direction==8 && key_order[1]==4) row_num[i]=6
else if (direction==8 && key_order[1]==1) row_num[i]=0
}
else row_num[i]=direction-1
}
}
function get_col_length() {
get_character_state()
get_row_num()
for (var i=0;i<characters.length;i++) {
col_length[i]=characters[i].dirs[row_num[i]][character_state].length-1
}
}
function find_direction() {
if (key_order[0]==1 && key_order[1]!=2 && key_order[1]!=4) direction=1
else if ((key_order[0]==1 && key_order[1]==2) || (key_order[0]==2 && key_order[1]==1)) direction=2
else if (key_order[0]==2 && key_order[1]!=1 && key_order[1]!=3) direction=3
else if ((key_order[0]==2 && key_order[1]==3) || (key_order[0]==3 && key_order[1]==2)) direction=4
else if (key_order[0]==3 && key_order[1]!=2 && key_order[1]!=4) direction=5
else if ((key_order[0]==3 && key_order[1]==4) || (key_order[0]==4 && key_order[1]==3)) direction=6
else if (key_order[0]==4 && key_order[1]!=3 && key_order[1]!=1) direction=7
else if ((key_order[0]==4 && key_order[1]==1) || (key_order[0]==1 && key_order[1]==4)) direction=8
else direction=0
}
function keydown(e) {
var keycode=getkeycode(e)
if (keycode==38 && !key_up) {
key_up=true
key_order.splice(0,0,1)
key_order.splice(3,1)
}
else if (keycode==39 && !key_right) {
key_right=true
key_order.splice(0,0,2)
key_order.splice(3,1)
}
else if (keycode==40 && !key_down) {
key_down=true
key_order.splice(0,0,3)
key_order.splice(3,1)
}
else if (keycode==37 && !key_left) {
key_left=true
key_order.splice(0,0,4)
key_order.splice(3,1)
}
else if (keycode==16) key_shift=true
else if (keycode==17) key_control=true
if (keycode==38 || keycode==39 || keycode==40 || keycode==37) find_direction()
if (keycode==38 || keycode==39 || keycode==40 || keycode==37 || keycode==16) get_col_length()
}
function keyup(e) {
var keycode=getkeycode(e)
if (keycode==38 && key_up) {
key_up=false
for (var i=0;i<4;i++) {
if (key_order[i]==1) key_order.splice(i,1)
}
key_order.splice(3,0,0)
}
else if (keycode==39 && key_right) {
key_right=false
for (var i=0;i<4;i++) {
if (key_order[i]==2) key_order.splice(i,1)
}
key_order.splice(3,0,0)
}
else if (keycode==40 && key_down) {
key_down=false
for (var i=0;i<4;i++) {
if (key_order[i]==3) key_order.splice(i,1)
}
key_order.splice(3,0,0)
}
else if (keycode==37 && key_left) {
key_left=false
for (var i=0;i<4;i++) {
if (key_order[i]==4) key_order.splice(i,1)
}
key_order.splice(3,0,0)
}
else if (keycode==16) key_shift=false
else if (keycode==17) key_control=false
if (key_order[0]>0 && (keycode==38 || keycode==39 || keycode==40 || keycode==37)) find_direction()
if (keycode==38 || keycode==39 || keycode==40 || keycode==37 || keycode==16) get_col_length()
}
function link(scr) {
current_screen=scr
}
function give_life(cha) {
col_num[characters.length]=0
characters[characters.length]=cha
get_col_length()
}
function take_life(cha) {
for (var i=0;i<characters.length;i++) {
if (characters[i]==cha) characters.splice(i,1)
}
}