⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 agent.java

📁 Multiagent system programmed using Java with Jason to simulate player interactivity in an MMORPG. Mu
💻 JAVA
字号:
//============================================================================================================================
// OpenGL Agent Class
// Author: Chris Willcocks
// Version: 1.0
//============================================================================================================================

import math.Vector2f;

public class agent extends start {

	public char type = 'N'; // (N)othing, (P)layer, (E)nemy
	public int health = 20; // Determines the agent health
	
	public Vector2f pos = new Vector2f(0.0f, 0.0f); // { x, y }
	public Vector2f fac = new Vector2f(0.0f, 0.0f); // Facing Direction
	private int rotate = 0; // Turn by how much? Mod 360

	public char behaviour = 'S'; // Behaviour Type:
	/*
	 * -> S = Static -> R = Random Turn -> L = Line Running -> Z = Teleporting
	 * -> T = Turning
	 */

	public float colour[] = new float[3];

	// ========================================================================================================================
	// CONSTRUCTORS
	// ========================================================================================================================
	agent(float x0, float y0) {
		pos.x = x0;
		pos.y = y0;
		behaviour = 'S';
		colour[0] = 0.5f;
		colour[1] = 1.0f;
		colour[2] = 0.5f;
	}

	agent() {
		pos.x = S_BORDER / 2 - (random.nextFloat() * S_BORDER);
		pos.y = S_BORDER / 2 - (random.nextFloat() * S_BORDER);
		pos.x += random.nextFloat();
		pos.y += random.nextFloat();
		rotate = random.nextInt() % 360;
		behaviour = 'R';
		colour[0] = 1.0f;
		colour[1] = 0.0f;
		colour[2] = 1.0f;
	}

	// ========================================================================================================================
	// METHODS
	// ========================================================================================================================
	private void Rotate(int r) {
		rotate += r;
		rotate = (int) rotate % 360;

		fac.x = (float) Math.sin(rotate * (Math.PI / 180.0f));
		fac.y = (float) Math.cos(rotate * (Math.PI / 180.0f));

		fac.normalize();
	}
	
	public void MoveTowards(Vector2f position, float d)
	{
		Vector2f D = new Vector2f();
		D.x = pos.x - position.x;
		D.y = pos.y - position.y;
		
		D.normalize();
		
		pos.x = pos.x - D.x * d;
		pos.y = pos.y - D.y * d;
		
		CheckBoundaries();
	}
	
	public boolean CollisionThreshold(Vector2f position, float threshold)
	{
		return Math.sqrt(Math.pow(pos.x-position.x, 2) + Math.pow(pos.y-position.y, 2)) <= threshold;
	}

	public void Update(float d) {
		if (behaviour == 'R') {
			Rotate(random.nextInt() % 20);
			pos.x = pos.x + fac.x * d;
			pos.y = pos.y + fac.y * d;
		} else if (behaviour == 'T') {
			Rotate((int) (random.nextInt() % 10.0f));
			pos.x = pos.x + fac.x * d;
			pos.y = pos.y + fac.y * d;
		} else if (behaviour == 'L') {
			Rotate(0);
			pos.x = pos.x + fac.x * d;
			pos.y = pos.y + fac.y * d;
		} else if (behaviour == 'Z') {
			pos.x = S_BORDER / 2 - (random.nextInt() % S_BORDER);
			pos.y = S_BORDER / 2 - (random.nextInt() % S_BORDER);
			pos.x += random.nextFloat();
			pos.y += random.nextFloat();
		}
		CheckBoundaries();
	}

	private void CheckBoundaries() {
		if (pos.x > S_BORDER / 2)
			pos.x = -S_BORDER / 2;

		else if (pos.x < -S_BORDER / 2)
			pos.x = S_BORDER / 2;

		if (pos.y > S_BORDER / 2)
			pos.y = -S_BORDER / 2;

		else if (pos.y < -S_BORDER / 2)
			pos.y = S_BORDER / 2;
	}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -