📄 infravision.java
字号:
package com.croftsoft.apps.infravision;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import com.croftsoft.core.gui.plot.PlotLib;
import com.croftsoft.core.lang.lifecycle.Lifecycle;
import com.croftsoft.core.math.RandomLib;
/*********************************************************************
* Goblins hunt kobolds in the dark using infravision.
*
* @version
* 2002-03-02
* @since
* 1996-08-23
* @author
* <a href="http://www.croftsoft.com/">David Wallace Croft</a>
*********************************************************************/
public class Infravision
extends Applet
implements Lifecycle, Runnable
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
{
boolean goblin_vision_on = false;
private Point lower_left = new Point ( 0, 0 );
private Point upper_right = new Point ( 100, 100 );
int walls_border_count = 2 * ( upper_right.x - lower_left.x + 1 )
+ 2 * ( upper_right.y - lower_left.y + 1 ) - 4;
int walls_count = walls_border_count + 100;
int goblins_count = 100;
int kobolds_count = 100;
boolean [ ] goblin_alive = new boolean [ goblins_count ];
boolean [ ] kobold_alive = new boolean [ kobolds_count ];
int goblins_alive_count = goblins_count;
int kobolds_alive_count = kobolds_count;
Point [ ] goblin_place = new Point [ goblins_count ];
Point [ ] kobold_place = new Point [ kobolds_count ];
Point [ ] wall_place = new Point [ walls_count ];
Thread runner;
Image offscreenImage;
Graphics offscreenGraphics;
static final int ETHER = 0;
static final int WALL = 1;
static final int GOBLIN = 2;
static final int KOBOLD = 3;
private int space_contents [ ] [ ]
= new int [ upper_right.x - lower_left.x + 1 ]
[ upper_right.y - lower_left.y + 1 ];
private Rectangle r = new Rectangle ( );
private Point margin_top_left = new Point ( 10, 10 );
private Point margin_bottom_right = new Point ( 20, 20 );
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public synchronized void init ( )
//////////////////////////////////////////////////////////////////////
{
r = new Rectangle ( margin_top_left.x, margin_top_left.y,
getSize ( ).width - margin_bottom_right.x,
getSize ( ).height - margin_bottom_right.y );
offscreenImage = createImage ( getSize ( ).width, getSize ( ).height );
offscreenGraphics = offscreenImage.getGraphics ( );
init_walls ( );
goblins_init ( );
kobolds_init ( );
}
public void start ( )
//////////////////////////////////////////////////////////////////////
{
runner = new Thread ( this );
int priority = runner.getPriority ( ) - 1;
if ( priority >= Thread.MIN_PRIORITY )
{
runner.setPriority ( priority );
}
runner.setDaemon ( true );
runner.start ( );
}
public synchronized void stop ( )
//////////////////////////////////////////////////////////////////////
{
Thread thread = runner;
if ( thread != null )
{
runner = null;
thread.interrupt ( );
}
}
public synchronized void destroy ( )
//////////////////////////////////////////////////////////////////////
{
stop ( );
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public void run ( )
//////////////////////////////////////////////////////////////////////
{
Thread thread = Thread.currentThread ( );
try
{
while ( thread == runner )
{
kobolds_move ( );
goblins_move ( );
paint ( offscreenGraphics );
Graphics g = this.getGraphics ( );
g.drawImage ( offscreenImage, 0, 0, this );
Thread.sleep ( 1000 );
}
}
catch ( InterruptedException e )
{
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public Point goblin_move_direction ( int index_goblin ) {
//////////////////////////////////////////////////////////////////////
Point direction = new Point ( 0, 0 );
Point proposed_location = new Point ( 0, 0 );
boolean target_found = false;
//////////////////////////////////////////////////////////////////////
if ( goblin_vision_on ) {
outer_loop:
for ( int index_x = -1;
index_x <= 1;
index_x++ ) {
for ( int index_y = -1;
index_y <= 1;
index_y++ ) {
proposed_location.x = goblin_place [ index_goblin ].x + index_x;
proposed_location.y = goblin_place [ index_goblin ].y + index_y;
if ( space_contents
[ proposed_location.x ]
[ proposed_location.y ] == KOBOLD ) {
target_found = true;
direction.x = index_x;
direction.y = index_y;
break outer_loop;
}
}
}
}
if ( !target_found ) {
direction.x = ( int ) RandomLib.roll ( 1, 3, -2 );
direction.y = ( int ) RandomLib.roll ( 1, 3, -2 );
}
return new Point ( direction.x, direction.y );
}
public void goblin_move ( int index_goblin ) {
//////////////////////////////////////////////////////////////////////
boolean abort_move = false;
Point move_direction;
Point new_place = new Point ( 0, 0 );
//////////////////////////////////////////////////////////////////////
new_place.x = goblin_place [ index_goblin ].x;
new_place.y = goblin_place [ index_goblin ].y;
move_direction = goblin_move_direction ( index_goblin );
new_place.x += move_direction.x;
new_place.y += move_direction.y;
if ( new_place.x < lower_left.x ) abort_move = true;
if ( new_place.x > upper_right.x ) abort_move = true;
if ( new_place.y < lower_left.y ) abort_move = true;
if ( new_place.y > upper_right.y ) abort_move = true;
if ( !abort_move ) {
if ( space_contents [ new_place.x ] [ new_place.y ] != ETHER ) {
if ( space_contents [ new_place.x ] [ new_place.y ] == KOBOLD ) {
kill_kobold ( new_place );
} else abort_move = true;
}
}
if ( !abort_move ) {
space_contents
[ goblin_place [ index_goblin ].x ]
[ goblin_place [ index_goblin ].y ] = ETHER;
goblin_place [ index_goblin ].x = new_place.x;
goblin_place [ index_goblin ].y = new_place.y;
space_contents [ new_place.x ] [ new_place.y ] = GOBLIN;
}
}
public void goblins_move ( ) {
//////////////////////////////////////////////////////////////////////
for ( int index_goblin = 0;
index_goblin < goblins_count;
index_goblin++ ) {
if ( goblin_alive [ index_goblin ] ) {
goblin_move ( index_goblin );
}
}
}
public Point kobold_move_direction ( int index_kobold ) {
//////////////////////////////////////////////////////////////////////
Point direction = new Point ( 0, 0 );
//////////////////////////////////////////////////////////////////////
direction.x = ( int ) RandomLib.roll ( 1, 3, -2 );
direction.y = ( int ) RandomLib.roll ( 1, 3, -2 );
return new Point ( direction.x, direction.y );
}
public void kobold_move ( int index_kobold ) {
//////////////////////////////////////////////////////////////////////
boolean abort_move = false;
Point move_direction;
Point new_place = new Point ( 0, 0 );
//////////////////////////////////////////////////////////////////////
new_place.x = kobold_place [ index_kobold ].x;
new_place.y = kobold_place [ index_kobold ].y;
move_direction = kobold_move_direction ( index_kobold );
new_place.x += move_direction.x;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -