/*
Eon Aton: a RPG-like game system in JavaScript
Copyright (C) 2008-2009 Niels Serup
This file is part of Eon Aton.
Eon Aton is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Eon Aton is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Eon Aton. If not, see .
*/
/*
This file contains various functions to be used together with the classes.
*/
function style_bg_img(background) {
var cssurl
if (background.indexOf(".")!=-1) cssurl=["url(",")"]
else cssurl=["",""]
return cssurl[0]+background+cssurl[1]
}
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
row_num=new Array()
col_num=new Array()
col_length=new Array()
maxwidth=0
maxheight=0
define_pixel_movement(5,10)
define_character_distance(-5)
direction_changing_speed=3
frame_showing_speed=150
movement_speed=50
frame_showing()
movement()
}
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 define_character_distance(dist) {
character_distance=dist
diagonal_character_distance=Math.sqrt(dist*dist*2)
if (dist<0) diagonal_character_distance=-diagonal_character_distance
}
function show_frame(cha_num,row,col) {
characters[cha_num].elem.style.backgroundPosition="-"+(col*(characters[cha_num].width+1)+1)+"px -"+(characters[cha_num].dirs[row][0]*(characters[cha_num].height+1)+1)+"px"
}
function frame_showing() {
for (var i=0;i-1 && x-1 && ycharacters[i].posY) cy--
}
characters[i].elem.style.zIndex=cy
li=i
}
if (characters.length>0) {
if (characters[0].posX>(current_screen.viswidth-characters[0].width)/2 && characters[0].posX(current_screen.visheight-characters[0].height)/2 && characters[0].posY0) character_state=2
else if (key_shift && key_order[0]>0) character_state=3
}
function get_row_num() {
for (var i=0;imaxwidth) maxwidth=characters[i].width
if (characters[i].height>maxheight) maxheight=characters[i].height
}
}
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 exchange_characters(a,b) {
var temp=characters[a]
characters[a]=characters[b]
characters[b]=temp
}
function give_life(cha) {
col_num[characters.length]=0
cha.visible=true
cha.elem.style.display="block"
get_col_length()
get_maxs()
}
function take_life(cha) {
cha.visible=false
cha.elem.style.display="none"
get_maxs()
}