actual raycasting for vision, talk functions / exchance colors
This commit is contained in:
200
CitizenClass.pde
200
CitizenClass.pde
@@ -1,9 +1,9 @@
|
||||
import java.lang.Double;
|
||||
int i_citizenAmount = 20;
|
||||
int i_citizenAmount = 100;
|
||||
int i_selectedCitizen;
|
||||
int i_femaleCount;
|
||||
|
||||
|
||||
boolean b_makeThemGo;
|
||||
//je nachdem wie hoch der derzeitige fokus ist wird es schwerer, ihn abzulenken.
|
||||
//intrinsischer fokus korreliert mit joy/motivation/positive erinnerung an etwas
|
||||
|
||||
@@ -25,21 +25,21 @@ void initCitizen() {
|
||||
//namen: ethuriel, nathaniel, loriel, samuel, aluriel, aleriel, thaliel, suriel, kaliel,
|
||||
//int[][] claireArray = { { 00, 2 }, { 1, 3} };
|
||||
|
||||
citizen[0] = new Citizen(800, "Test", 0, 27);
|
||||
citizen[1] = new Citizen(121, "Matze", 1, 28);
|
||||
citizen[2] = new Citizen(18, "Freddy", 1, 28);
|
||||
citizen[3] = new Citizen(59, "Korbi", 1, 28);
|
||||
citizen[4] = new Citizen(121, "Lotte", 1, 26);
|
||||
citizen[5] = new Citizen(18, "Reifi", 1, 26);
|
||||
citizen[6] = new Citizen(59, "Grasso", 1, 28);
|
||||
citizen[7] = new Citizen(121, "Nico", 1, 29);
|
||||
citizen[8] = new Citizen(18, "Victor", 1, 28);
|
||||
citizen[9] = new Citizen(59, "Paul", 1, 28);
|
||||
citizen[0] = new Citizen(0, 800, "Test", 0, 27);
|
||||
citizen[1] = new Citizen(1, 121, "Matze", 1, 28);
|
||||
citizen[2] = new Citizen(2, 18, "Freddy", 1, 28);
|
||||
citizen[3] = new Citizen(3, 59, "Korbi", 1, 28);
|
||||
citizen[4] = new Citizen(4, 121, "Lotte", 1, 26);
|
||||
citizen[5] = new Citizen(5, 18, "Reifi", 1, 26);
|
||||
citizen[6] = new Citizen(6, 59, "Grasso", 1, 28);
|
||||
citizen[7] = new Citizen(7, 121, "Nico", 1, 29);
|
||||
citizen[8] = new Citizen(8, 18, "Victor", 1, 28);
|
||||
citizen[9] = new Citizen(9, 59, "Paul", 1, 28);
|
||||
for (int i = 10; i < i_citizenAmount; i++) {
|
||||
int gender = int(random(0, 2));
|
||||
if (gender == 0) i_femaleCount++;
|
||||
int age = int(random(0, 100));
|
||||
citizen[i] = new Citizen(int(random(0, houses.size())), S_citizenNames[gender][int(random(0, S_citizenNames[gender].length))], gender, age);
|
||||
citizen[i] = new Citizen(i, int(random(0, houses.size())), S_citizenNames[gender][int(random(0, S_citizenNames[gender].length))], gender, age);
|
||||
}
|
||||
for (int i = 0; i < citizen.length; i++) {
|
||||
if (i != i_selectedCitizen) //hacky to set citizen[0] to a specific place
|
||||
@@ -71,8 +71,18 @@ class Citizen extends Entity {
|
||||
String s_currentAction;
|
||||
float f_movementSpeed;
|
||||
float f_FOV = PI / 4.0;
|
||||
int i_visionDistance = 60;
|
||||
int i_visionDistance = 120;
|
||||
int i_lookRaySteps = 10;
|
||||
int i_id;
|
||||
|
||||
int i_perceivedCitizen = -1;
|
||||
boolean b_perceivesCitizen;
|
||||
|
||||
boolean b_randomRun = b_autoPlay;
|
||||
|
||||
PVector dot = new PVector(0, 0);
|
||||
|
||||
|
||||
GraphNode startNode, endNode;
|
||||
int startNodeID, endNodeID;
|
||||
GraphNode[] rNodes;
|
||||
@@ -82,7 +92,8 @@ class Citizen extends Entity {
|
||||
IGraphSearch pathFinder;
|
||||
int pathAlgorithm = i_pathAlgorithm;
|
||||
|
||||
Citizen(int home, String name, int gender, int age) {
|
||||
Citizen(int id, int home, String name, int gender, int age) {
|
||||
i_id = id;
|
||||
i_home = home;
|
||||
s_name = name;
|
||||
i_gender = gender;
|
||||
@@ -93,11 +104,11 @@ class Citizen extends Entity {
|
||||
f_movementSpeed = random(f_walkSpeedMin, f_walkSpeedMax);
|
||||
rNodes = pathFinder.getRoute();
|
||||
s_currentAction = "current action here";
|
||||
c_color = color((int)(random(0, 255)), (int)(random(0, 255)), (int)(random(0, 255)));
|
||||
c_selected = color(128, 50, 50);
|
||||
c_color = color(0, 0, 0);
|
||||
c_stroke = color(255, 255, 255);
|
||||
|
||||
if (b_randomRun) goTo(int(random(0, pg_map.width)), int(random(0, pg_map.height)), 10); //bad
|
||||
//if (b_randomRun) goTo(int(random(0, pg_map.width)), int(random(0, pg_map.height)), 10); //bad
|
||||
}
|
||||
//citizen kann "lookat" mit 180° winkel mit bullseye förmigen gradient welches die chance etwas zu bemerken darstellt
|
||||
//setFocus(entity) durch geräusche auch von objekten ausserhalb des sichtfeldes aufrufbar
|
||||
@@ -116,36 +127,143 @@ class Citizen extends Entity {
|
||||
void update() {
|
||||
f_angle = v2_direction.heading();
|
||||
|
||||
i_collBoundX = (int)f_xPos - (i_diameter/2);
|
||||
i_collBoundY = (int)f_yPos - (i_diameter/2);
|
||||
i_collBoundW = i_diameter;
|
||||
i_collBoundH = i_diameter;
|
||||
|
||||
if (b_turning) {
|
||||
turn();
|
||||
}
|
||||
|
||||
if (b_moving) { //if rNodes.length > 0
|
||||
move();
|
||||
}
|
||||
|
||||
perceive();
|
||||
if (b_talking) {
|
||||
talk();
|
||||
}
|
||||
}
|
||||
|
||||
void display() {
|
||||
pg_map.stroke(c_stroke);
|
||||
pg_map.fill(c_color);
|
||||
//if (b_selected) pg_map.fill(c_selected);
|
||||
pg_map.ellipse(f_xPos, f_yPos, i_diameter, i_diameter);
|
||||
pg_map.fill(c_stroke);
|
||||
pg_map.textAlign(CENTER);
|
||||
pg_map.textSize(i_nameSize);
|
||||
pg_map.text(s_name, f_xPos, f_yPos-10);
|
||||
if (b_perceivesCitizen) {
|
||||
pg_map.text("!", f_xPos, f_yPos-20);
|
||||
}
|
||||
|
||||
if (b_talking) {
|
||||
pg_map.line(f_xPos, f_yPos, citizen[i_lastCitizenTalked].f_xPos, citizen[i_lastCitizenTalked].f_yPos);
|
||||
}
|
||||
}
|
||||
|
||||
void perceive() {
|
||||
b_perceivesCitizen = false;
|
||||
i_perceivedCitizen = -1;
|
||||
if (!b_talking) {
|
||||
for (int i = -5; i < 6; i++) { //10 rays
|
||||
float f_rayAngle = (f_angle) + i/TWO_PI;
|
||||
for (int j = 0; j < 10; j++) { //10 steps of collision detection per ray
|
||||
for (int k = 1; k < i_lookRaySteps; k ++) {
|
||||
dot.x = f_xPos-k*((cos(f_rayAngle)*i_visionDistance)/i_lookRaySteps);
|
||||
dot.y = f_yPos-k*((sin(f_rayAngle)*i_visionDistance)/i_lookRaySteps);
|
||||
if (b_debug)pg_map.point(dot.x, dot.y);
|
||||
for (int l = 0; l < citizen.length; l++) {
|
||||
if (dot.x > citizen[l].i_collBoundX && dot.x < citizen[l].i_collBoundX + citizen[l].i_collBoundW
|
||||
&& dot.y > citizen[l].i_collBoundY && dot.y < citizen[l].i_collBoundY + citizen[l].i_collBoundH) {
|
||||
if (l != i_id && !b_perceivesCitizen) {
|
||||
b_perceivesCitizen = true;
|
||||
i_perceivedCitizen = l;
|
||||
|
||||
turnTo(f_xPos-citizen[l].f_xPos, f_yPos-citizen[l].f_yPos, 2);
|
||||
|
||||
if (citizen[l].i_perceivedCitizen == i_id && i_lastCitizenTalked != l && citizen[l].i_lastCitizenTalked != i_id) { //if perceived citizen perceives me and din't talk to me last time
|
||||
talkTo(citizen[l]);
|
||||
citizen[l].talkTo(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean b_talking;
|
||||
boolean b_movingBeforeTalk;
|
||||
int i_talkTimeStart;
|
||||
int i_talkTime = 5; //how long talk in seconds (better in frames?)
|
||||
int i_lastCitizenTalked = -1;
|
||||
|
||||
void talkTo(Citizen citizen) { //replace passing whole entity with passing id
|
||||
b_talking = true;
|
||||
//i_talkTimeStart = second();
|
||||
i_talkTimeStart = frameCount;
|
||||
b_movingBeforeTalk = b_moving;
|
||||
b_moving = false;
|
||||
i_lastCitizenTalked = citizen.i_id;
|
||||
|
||||
println(s_name + " talking to " + citizen.s_name);
|
||||
//c_color = c_color / 10 * 9 + citizen.c_color/10;
|
||||
}
|
||||
|
||||
void talk() { //means: exchanging colors
|
||||
float r = red(c_color);
|
||||
float g = green(c_color);
|
||||
float b = blue(c_color);
|
||||
|
||||
|
||||
float r_new = (99*r + red(citizen[i_lastCitizenTalked].c_color)) / 99.6666;
|
||||
float g_new = (99*g + green(citizen[i_lastCitizenTalked].c_color)) / 99.6666;
|
||||
float b_new = (99*b + blue(citizen[i_lastCitizenTalked].c_color)) / 99.6666;
|
||||
|
||||
c_color = color(r_new, g_new, b_new);
|
||||
|
||||
//if (second() > (i_talkTimeStart + i_talkTime < 60 ? i_talkTimeStart + i_talkTime : i_talkTimeStart - 60 + i_talkTime)) {
|
||||
if (frameCount > i_talkTimeStart + i_talkTime * frameRate) {
|
||||
|
||||
//c_color = (c_color + citizen[i_lastCitizenTalked].c_color) / 2;
|
||||
b_talking = false;
|
||||
b_moving = b_movingBeforeTalk;
|
||||
//prevent talking to the same person right after
|
||||
}
|
||||
}
|
||||
boolean b_pathFindKaputt;
|
||||
|
||||
void goTo(int x, int y, int prio) {
|
||||
//add: go to node? go to house?
|
||||
int i_goToX = x;
|
||||
int i_goToY = y;
|
||||
int spread = 0;
|
||||
boolean findingWay = false;
|
||||
|
||||
|
||||
|
||||
|
||||
while (!findingWay) {
|
||||
while (!findingWay && !b_pathFindKaputt) {
|
||||
float nodeDistance = 1.0f;
|
||||
findingWay = true;
|
||||
while (gs.getNodeAt(f_xPos, f_yPos, 0, nodeDistance) == null) {
|
||||
|
||||
while (gs.getNodeAt(f_xPos, f_yPos, 0, nodeDistance) == null && nodeDistance < 1000) {
|
||||
nodeDistance++;
|
||||
}
|
||||
if (nodeDistance>= 1000) {
|
||||
b_pathFindKaputt = true;
|
||||
break;
|
||||
}
|
||||
startNodeID = gs.getNodeAt(f_xPos, f_yPos, 0, nodeDistance).id();
|
||||
|
||||
nodeDistance = 1.0f;
|
||||
while (gs.getNodeAt(i_goToX, i_goToY, 0, nodeDistance) == null) {
|
||||
while (gs.getNodeAt(i_goToX, i_goToY, 0, nodeDistance) == null && nodeDistance < 1000) {
|
||||
nodeDistance++;
|
||||
}
|
||||
if (nodeDistance >= 1000) {
|
||||
b_pathFindKaputt = true;
|
||||
break;
|
||||
}
|
||||
endNodeID = gs.getNodeAt(i_goToX, i_goToY, 0, nodeDistance).id();
|
||||
|
||||
if (pathFinder.search(startNodeID, endNodeID, true) == null) {
|
||||
@@ -154,15 +272,20 @@ class Citizen extends Entity {
|
||||
i_goToX+= spread;
|
||||
i_goToY+= spread;
|
||||
}
|
||||
if (spread >= 1000) b_pathFindKaputt = true;
|
||||
}
|
||||
pathFinder.search(startNodeID, endNodeID, true); //returns null if no route is found
|
||||
rNodes = pathFinder.getRoute();
|
||||
if (rNodes.length > 0) {
|
||||
nextNode = 0;
|
||||
b_moving = true;
|
||||
} else { //implement more intelligent design which calls goTo not in this frame (this can cause infinite loops), but in the next 120 frames or so
|
||||
//also at this point i can move recalculating the nodes into the next frames if no route is found (not only random)..
|
||||
if (b_randomRun) goTo(int(random(0, pg_map.width)), int(random(0, pg_map.height)), 10);
|
||||
if (!b_pathFindKaputt) {
|
||||
pathFinder.search(startNodeID, endNodeID, true); //returns null if no route is found
|
||||
rNodes = pathFinder.getRoute();
|
||||
if (rNodes.length > 0) {
|
||||
nextNode = 0;
|
||||
b_moving = true;
|
||||
} else { //implement more intelligent design which calls goTo not in this frame, but in the next 120 frames or so
|
||||
//also at this point i can move recalculating the nodes into the next frames if no route is found (not only random)..
|
||||
if (b_randomRun) goTo(int(random(0, pg_map.width)), int(random(0, pg_map.height)), 10);
|
||||
}
|
||||
} else {
|
||||
println("Pathfinding failed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,7 +299,7 @@ class Citizen extends Entity {
|
||||
f_xPos += i_direction(f_xPosNext-f_xPos, f_movementVelo) * f_movementVelo;
|
||||
f_yPos += i_direction(f_yPosNext-f_yPos, f_movementVelo) * f_movementVelo;
|
||||
|
||||
turnTo(f_xPos-f_xPosNext, f_yPos-f_yPosNext, .3);
|
||||
turnTo(f_xPos-f_xPosNext, f_yPos-f_yPosNext, 1);
|
||||
|
||||
if (abs(f_yPos - f_yPosNext) < f_movementVelo && abs(f_xPos - f_xPosNext) < f_movementVelo) {
|
||||
f_yPos = f_yPosNext;
|
||||
@@ -211,15 +334,6 @@ class Citizen extends Entity {
|
||||
pg_map.ellipse(rNodes[rNodes.length-1].xf(), rNodes[rNodes.length-1].yf(), nodeSize, nodeSize);
|
||||
pg_map.popStyle();
|
||||
}
|
||||
|
||||
|
||||
pg_map.stroke(color(100, 50, 50));
|
||||
for (int i = -5; i < 6; i++) { //16 rays
|
||||
float fRayAngle = (f_angle) + i/TWO_PI;
|
||||
pg_map.line(f_xPos, f_yPos, f_xPos-cos(fRayAngle)*i_visionDistance, f_yPos-sin(fRayAngle)*i_visionDistance);
|
||||
}
|
||||
pg_map.stroke(color(255, 255, 255));
|
||||
pg_map.line(f_xPos, f_yPos, f_xPos-cos(f_angle)*i_visionDistance, f_yPos-sin(f_angle)*i_visionDistance);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user