package examples; import jgame.*; import jgame.platform.*; /** A Gauntlet clone with generated levels. */ public class DungeonsOfHack extends StdGame { // object cids: 1=player 2=monster 4=bullet 8=monsterbullet public static final int WALL_T=1; public static final int SHWALL_T=2; // shootable wall public static final int DOOR_T=4; public static final int PLAYER_T=16; public static final int MONSTER_T=32; public static final int GEN_T=64; // generator public static final int BONUS_T=128; public static final int KEY_T=256; public static final int HEALTH_T=512; public static final int PLAYERBLOCK_T = WALL_T|SHWALL_T|DOOR_T|GEN_T; public static final int MONSTERBLOCK_T = WALL_T|SHWALL_T|DOOR_T|BONUS_T|MONSTER_T|GEN_T|BONUS_T|KEY_T|HEALTH_T; public static final int BULLETBLOCK_T = WALL_T|DOOR_T|BONUS_T|BONUS_T|KEY_T|HEALTH_T; public static void main(String [] args) { if (args.length<1) { System.out.println( "Please supply \"scroll\" or \"noscroll\" as first argument."); System.exit(0); } boolean do_scroll=false; if (args[0].equals("scroll")) { do_scroll=true; } new DungeonsOfHack(parseSizeArgs(args,1),do_scroll); } boolean do_scroll=false; public DungeonsOfHack() { initEngineApplet(); } public DungeonsOfHack(JGPoint size,boolean do_scroll) { this.do_scroll=do_scroll; initEngine(size.x,size.y); } public void initCanvas() { if (isApplet()) do_scroll = getParameter("scrolling")!=null; if (isMidlet()) do_scroll = true; if (isMidlet()) { setCanvasSettings(18,18,10,10,null,null,null); setScalingPreferences(3.0/4.0,4.0/3.0, 0,4,0,4); } else if (do_scroll) { setCanvasSettings(20,15,10,10,null,null,null); } else { setCanvasSettings(60,40,10,10,null,null,null); } } public void initGame() { defineMedia("dungeons_of_hack.tbl"); setVideoSyncedUpdate(true); if (isMidlet()) { setFrameRate(10,1); setGameSpeed(1.8); } else { setFrameRate(30,4); } startgame_ticks=1000; // wait for awhile so player can look around gameover_ingame=true; title_font = new JGFont("Helvetica",JGFont.BOLD,17); setPFSize(60,40); } JGObject player=null; int keys=0,health=0, kills=0, gold_left=0; public void initNewGame(int level_selected) { health=10; keys=0; level=0; kills=0; } public void defineLevel() { fillBG(""); removeObjects(null,0); player=new Player(10,10,5,key_up,key_down,key_left,key_right); setViewOffset((int)player.x-8,(int)player.y-8,true); // define segment walls int [] walls_x, walls_y; int [] [] cells; // indicates cells' exits //double openness = 0.2; double door_rate = random(0.0,1.0); double shootablewall_rate = random(0.0,0.6); int nr_keys,nr_bonuses,nr_health,nr_demongen,nr_mushroomgen,nr_turrets; int turret_firerate,mushroomrate,demonrate,demon_firerate; if (random(0,1) > 0.42) { // open dungeon walls_x = defineWallPos(2,8,pfTilesX()-1,true); walls_y = defineWallPos(2,8,pfTilesY()-1,true); cells = defineCells(walls_x.length+1,walls_y.length+1, random(0.0,0.1) ); nr_keys = 4; nr_bonuses=10; nr_health=5; nr_demongen=5; nr_mushroomgen=10; nr_turrets=10; mushroomrate=80; demonrate=100; turret_firerate=200; demon_firerate=250; } else { // maze type dungeon; about 2.5 times more stuff walls_x = defineWallPos(9,16,pfTilesX()-1,false); walls_y = defineWallPos(9,16,pfTilesY()-1,false); cells = defineCells(walls_x.length+1,walls_y.length+1, random(0.0,0.15) ); nr_keys = 10; nr_bonuses=25; nr_health=12; nr_demongen=12; nr_mushroomgen=25; nr_turrets=25; mushroomrate=140; demonrate=200; // fire faster because it's easier to hide turret_firerate=120; demon_firerate=180; } // base walls for (int x=0; x=cells.length || y+ydir>=cells[0].length) continue; // is that cell already dug? if (and(cells[x+xdir][y+ydir],16) && random(0.001,1.0) > openness ) continue; // dig opening cells[x][y] |= try_dirs[i]; cells[x+xdir][y+ydir] |= otherdir; digMazePath(cells,x+xdir,y+ydir,openness); } } public int [] defineWallPos(int minwalls,int maxwalls, int maxpos, boolean random_pos){ int [] wallpos = new int [random(minwalls,maxwalls,1)]; // set to average position for (int i=0; i 150) { avoid=false; } else { avoid=true; } } if ( (firecount--) <= 0) { firecount=firerate; if (player!=null) { double angle = atan2(player.x-x,player.y-y); new TurretBullet(x, y, 1.0*Math.sin(angle), 1.0*Math.cos(angle), 120); } } } } //AudioClip aud = newAudioClip(getClass().getClassLoader().getResource("sounds/spacemusic.au")); public class Player extends StdDungeonPlayer { public Player(double x,double y,double speed,int upkey,int downkey, int leftkey,int rightkey) { super("player",x,y,1,"human_l",false,false, PLAYERBLOCK_T,PLAYER_T, 2.3, upkey,downkey,leftkey,rightkey); stopAnim(); } int prevxdir=1,prevydir=0; public void move() { int checkxdir=0,checkydir=0; if (getKey(key_left)) { checkxdir = -1; prevxdir = -1; prevydir = 0; } if (getKey(key_right)) { checkxdir = 1; prevxdir = 1; prevydir = 0; } if (getKey(key_up)) { checkydir = -1; prevxdir = 0; prevydir = -1; } if (getKey(key_down)) { checkydir = 1; prevxdir = 0; prevydir = 1; } if (getKey(key_fire)) { stop_moving=true; JGPoint cen = getCenterTile(); if (and(getTileCid(cen,prevxdir,prevydir),DOOR_T)) { // player is pushing against door if (keys > 0) { openDoor(cen.x+prevxdir,cen.y+prevydir); keys--; } } } else { stop_moving=false; } checkxdir=0; checkydir=0; if (getKey(key_fireleft)) checkxdir = -1; if (getKey(key_fireright)) checkxdir = 1; if (getKey(key_fireup)) checkydir = -1; if (getKey(key_firedown)) checkydir = 1; if (countObjects("bullet",0) < 1 && (checkxdir!=0 || checkydir!=0) ) { new Bullet(x,y, checkxdir,checkydir, 6.0); } super.move(); if (xdir > 0) setGraphic("human_r"); if (xdir < 0) setGraphic("human_l"); } public void hit_bg(int tilecid, int tx,int ty) { if (and(tilecid,BONUS_T)) { setTile(tx,ty,""); score += 1; gold_left--; } else if (and(tilecid,KEY_T)) { keys++; setTile(tx,ty,""); } else if (and(tilecid,HEALTH_T)) { if (health < 15) { health+=2; setTile(tx,ty,""); new StdScoring("msg",x,y-10,0,-0.5,40,"Potion! +2 health", new JGFont("Helvetica",0,10), new JGColor[] {JGColor.white,JGColor.red},2); } } } public void hit(JGObject obj) { if (obj.colid == 4) { // monster obj.remove(); health -= 2; new StdScoring("msg",x,y-10,0,-0.5,40,"Hit! -2 health", new JGFont("Helvetica",0,10), new JGColor[] {JGColor.white,JGColor.magenta},2); } else if (obj.colid == 8) { // bullet //aud.play(); obj.remove(); health -= 1; new StdScoring("msg",x,y-10,0,-0.5,40,"Shot! -1 health", new JGFont("Helvetica",0,10), new JGColor[] {JGColor.white,JGColor.blue},2); } } } void openDoor(int x,int y) { if (!and(getTileCid(x,y),DOOR_T)) return; setTile(x,y,""); if (x>0) openDoor(x-1,y); if (y>0) openDoor(x,y-1); if (x