aboutsummaryrefslogtreecommitdiffstats
path: root/src/entities.cpp
blob: 9f5d776dba05f723e8397ba22a0929b7fc4fdb68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <entities.h>

extern std::vector<Entity*>entity;
extern std::vector<NPC>npc;
extern std::vector<Structures>build;

void Entity::spawn(float x, float y){	//spawns the entity you pass to it based off of coords and global entity settings
	loc.x = x;
	loc.y = y;
	vel.x = 0;
	vel.y = 0;
	right = false;
	left = false;
	near = false;
	ticksToUse = 0;
	canMove = false;
	ground = false;
	name = (char*)malloc(16);
	getName();
}

void Entity::draw(void){		//draws the entities
	if(type==NPCT){
		if(gender == MALE)
			glColor3ub(0,0,100);
		else if(gender == FEMALE)
			glColor3ub(255,105,180);
	}else if(type==STRUCTURET){
		glColor3ub(100,0,100);
	}
	glRectf(loc.x,loc.y,loc.x+width,loc.y+height);
}

void Entity::wander(int timeRun, vec2 *v){ //this makes the entites wander about
	static int direction;	//variable to decide what direction the entity moves
	if(ticksToUse == 0){
		ticksToUse = timeRun;
		v->x = .008*HLINE;	//sets the inital velocity of the entity
		direction = (getRand() % 3 - 1); 	//sets the direction to either -1, 0, 1
											//this lets the entity move left, right, or stay still
		v->x *= direction;	//changes the velocity based off of the direction
	}
	ticksToUse--; //removes one off of the entities timer
}

void Entity::getName(){
	rewind(names);
	char buf;
	char* bufs = (char*)malloc(16);
	int max = 0;
	int tempNum = rand()%105;
	for(int i=0;i<tempNum;i++){
		fgets(bufs,16,(FILE*)names);
	}
	buf = fgetc(names);
	if(buf == 'm'){
		gender = MALE;
		std::puts("Male");
	}else if(buf == 'f'){
		gender = FEMALE;
		std::puts("Female");
	}
	if((fgets(bufs,16,(FILE*)names)) != NULL){
		std::puts(bufs);
		bufs[strlen(bufs)-1] = 0;
		strcpy(name,bufs);
	}
	free(bufs);
}

Player::Player(){ //sets all of the player specific traits on object creation
	width = HLINE * 8;
	height = HLINE * 12;
	speed = 1;
	type = PLAYERT; //set type to player
	subtype = 5;
	alive = true;
	ground = false;
}

void Player::interact(){ //the function that will cause the player to search for things to interact with
	
}

NPC::NPC(){	//sets all of the NPC specific traits on object creation
	width = HLINE * 8;
	height = HLINE * 12;
	speed = 1;
	type = NPCT; //sets type to npc
	subtype = 0;
	alive = true;
	canMove = true;
	near = false;
}

void NPC::addAIFunc(int (*func)(NPC *)){
	aiFunc.push_back(func);
}

void NPC::interact(){ //have the npc's interact back to the player
	int (*func)(NPC *);
	loc.y += 5;
	if(aiFunc.size()){
		func=aiFunc.front();
		if(!func(this)){
			aiFunc.erase(aiFunc.begin());
		}
	}
}

Structures::Structures(){ //sets the structure type
	type = STRUCTURET;
	speed = 0;
	alive = true;
	near = false;
}

unsigned int Structures::spawn(_TYPE t, float x, float y){ //spawns a structure based off of type and coords
	loc.x = x;
	loc.y = y;
	type = t;

	/*VILLAGE*/
	//spawns a village
	//spawns between 1 and 5 villagers around the village
	if(type == STRUCTURET){
		loc.y=100;
		width =  20 * HLINE;
		height = 16 * HLINE;

		int tempN = (getRand() % 5 + 1); //amount of villagers that will spawn
		for(int i=0;i<tempN;i++){
			entity.push_back(new NPC()); //create a new entity of NPC type
			npc.push_back(NPC()); //create new NPC
			entity[entity.size()] = &npc[npc.size()-1]; //set the new entity to have the same traits as an NPC
			entity[entity.size()-1]->spawn(loc.x + (float)(i - 5),100); //sets the position of the villager around the village
		}
		return entity.size();
	}
}