-bird = {
+player = {
Player = 0,
EventListeners = {
MoveLeftPressed = function(self)
- self.Velocity.x = self.Velocity.x - 100
+ self.Velocity.x = self.Velocity.x - 3
self.Render.flipx = true;
end,
MoveLeftReleased = function(self)
- self.Velocity.x = self.Velocity.x + 100
+ self.Velocity.x = self.Velocity.x + 3
end,
MoveRightPressed = function(self)
- self.Velocity.x = self.Velocity.x + 100
+ self.Velocity.x = self.Velocity.x + 3
self.Render.flipx = false;
end,
MoveRightReleased = function(self)
- self.Velocity.x = self.Velocity.x - 100
+ self.Velocity.x = self.Velocity.x - 3
end,
JumpKeyPressed = function(self)
if self.Physics.standing == true then
- self.Velocity.y = self.Velocity.y + 210
+ self.Velocity.y = self.Velocity.y + 9
end
end,
JumpKeyReleased = function(self)
end
},
Position = {
- x = 150,
+ x = 15,
y = 75
},
Velocity = {
visibleTick = 0
}
-cat = {
- Velocity = {
- x = 0.0,
- y = 0.0
- },
- Position = {
- x = 180,
- y = -75
- },
- Render = {
- texture = "Assets/cat.png",
- normal = "Assets/cat_normal.png",
- visible = true
- },
- counter = 0;
- Idle = function(self)
- self.Velocity.x = -100 * math.sin(math.rad(self.counter));
- self.Velocity.y = 100 * math.cos(math.rad(self.counter));
- self.counter = self.counter + 5;
- end,
- PhysicsIdle = function(self)
- if self.Velocity.x < 0 then
- self.Render.flipx = true
- elseif self.Velocity.x > 0 then
- self.Render.flipx = false
- end
- end
-}
-
-animal = {
- Name = "animal",
- Velocity = { -- This will automatically assign position to (0,0)
- x = 0.0,
- y = 0.0
- },
- Render = {
- texture = "Assets/rabbit.png",
- visible = true
- },
- Light = {
- r = 0.0,
- b = 1.0,
- g = 0.2,
- strength = 1.0
- },
- Idle = function(self)
- self.Velocity.x = -200 * math.sin(math.rad(self.counter));
- self.Velocity.y = 500 * math.cos(math.rad(self.counter*5));
- self.counter = self.counter + 5;
- end,
- counter = 0,
- TestMe = function(self)
- print("Light: rgb("..self.Light.r..","..self.Light.g..","..self.Light.b..")")
- end
-}
-
-wall = {
- Position = {
- y = -100
- },
- Render = {
- texture = "Assets/rock.png",
- normal = "Assets/rock_normal.png",
- visible = true
- }
-}
-
-- Create the world
dofile("Scripts/world.lua")
-birdSpawn = game.spawn(bird);
-
-dogSpawn = game.spawn(cat);
-
-animalSpawn = game.spawn(animal);
-
-wallSpawn = game.spawn(wall);
-
-game.spawn({
- Velocity = {
- x = 0,
- y = 0
- },
- Light = {
- r = 0.8,
- g = 0.1,
- b = 0.2,
- strength = 0.75
- },
- counter = 0;
- Idle = function(self)
- self.Velocity.x = -100 * math.sin(math.rad(self.counter));
- self.Velocity.y = 100 * math.cos(math.rad(self.counter));
- self.counter = self.counter + 5;
- end
-});
+playerSpawn = game.spawn(player);
-------------------
-- SERIALIZING --
-------------------
-function serial (before_ser)
- binary = string.dump(before_ser)
- formatted_binary = ""
- for i = 1, string.len(binary) do
- dec, _ = ("\\%3d"):format(binary:sub(i, i):byte()):gsub(' ', '0')
- formatted_binary = formatted_binary .. dec
- end
- return formatted_binary
-end
-
-function hello(herro)
- print("Hello world ".. herro)
-end
-
---print(serial(hello))
-
-local ser = string.dump(hello)
-f2 = (loadstring or load)(ser)
-f2("shite")
-
-local testPut = string.dump(animalSpawn.TestMe)
-testRun = (loadstring or load)(testPut)
-testRun(animalSpawn)
+--function serial (before_ser)
+-- binary = string.dump(before_ser)
+-- formatted_binary = ""
+-- for i = 1, string.len(binary) do
+-- dec, _ = ("\\%3d"):format(binary:sub(i, i):byte()):gsub(' ', '0')
+-- formatted_binary = formatted_binary .. dec
+-- end
+-- return formatted_binary
+--end
+--
+--function hello(herro)
+-- print("Hello world ".. herro)
+--end
+--
+----print(serial(hello))
+--
+--local ser = string.dump(hello)
+--f2 = (loadstring or load)(ser)
+--f2("shite")
+--
+--local testPut = string.dump(animalSpawn.TestMe)
+--testRun = (loadstring or load)(testPut)
+--testRun(animalSpawn)
Generate = function(self)
math.randomseed(self.Seed)
xsize, ysize, zsize = self:setSize(250, 128, 3)
- --self.data = {}
for Z = 0,zsize-1 do
- --self.data[Z] = {}
for X = 0,xsize-1 do
- --self.data[Z][X] = {}
- YGen = math.floor(6*math.sin(X/20) + Z) + 64
- YDepth = math.random(2,5)
+ if Z == 0 then
+ YGen = math.floor(6*math.sin(X/20)) + 64
+ elseif Z == 1 then
+ YGen = math.floor(9*math.sin(X/20)) + 64
+ else
+ YGen = math.floor(15*math.sin(X/20)) + 64
+ end
+ YDepth = math.random(3,5)
for Y = 0,ysize-1 do
if Y == YGen then
self:setData(X, Y, Z, "grass");
elseif Y < YGen and Y > (YGen - YDepth) then
self:setData(X, Y, Z, "dirt");
elseif Y < YGen then
- --self:setData(X, Y, Z, "stone");
- self:setData(X, Y, Z, "grass");
+ self:setData(X, Y, Z, "stone");
+ elseif Y == YGen + 1 then
+ if math.random(0, 100) == 53 then
+ self:setData(X, Y, Z, "flower");
+ end
end
--print(X..","..Y..","..Z);
end
void main()
{
// Quadratic light falloff
- vec3 Falloff = vec3(0.1, 0.2, 0.0);
+ vec3 Falloff = vec3(0.4, 0.1, 0.002);
vec4 DiffuseColor = texture2D(textu, texCoord);
vec3 SumLight = vec3(0.0);
for (int i = 0; i < LightNum; i++) {
- vec3 LightDir = vec3(LightPos[i].xy - fragCoord.xy, LightPos[i].z);
+ vec3 LightDir = vec3(LightPos[i].xy - fragCoord.xy,
+ (LightPos[i].z - fragCoord.z)*2);
float D = length(LightDir);
*/
#include "physics.hpp"
+
#include "components/Physics.hpp"
#include "components/Position.hpp"
#include "components/Velocity.hpp"
void PhysicsSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
[[maybe_unused]]entityx::EventManager& events)
{
-
+ events.subscribe<WorldChangeEvent>(*this);
}
void PhysicsSystem::update([[maybe_unused]]entityx::EntityManager& entities,
[[maybe_unused]]entityx::TimeDelta dt)
{
entities.each<Position, Velocity>
- ([dt](entityx::Entity e, Position &pos, Velocity &vel){
+ ([this, dt](entityx::Entity e, Position &pos, Velocity &vel){
bool has_phys = e.has_component<Physics>();
pos.x += (vel.x * dt/1000.0);
pos.y += (vel.y * dt/1000.0);
- double fallPosition = 0;
-
// TODO make this intersect world instead of 0 y
if (has_phys) {
+
+ double fallPosition = currentWorld->getHeight(pos.x, pos.y, 0.0);
+
Physics *p = e.component<Physics>().get();
// TODO only make this occur when the entity has a hitbox
if (pos.y == fallPosition) {
p->standing = true;
} else {
p->standing = false;
- vel.y -= 920 * (dt/1000.0f);
+ vel.y -= 32.2 * (dt/1000.0f);
}
}
});
}
+
+void PhysicsSystem::receive(const WorldChangeEvent& wce)
+{
+ currentWorld = wce.newWorld;
+}
#include <entityx/entityx.h>
#include <sol/sol.hpp>
+#include "world.hpp"
+#include "events/world.hpp"
+
/**
* @class PhysicsSystem
* Handles the position and velocity updating of all entities
*/
-class PhysicsSystem : public entityx::System<PhysicsSystem>
+class PhysicsSystem : public entityx::System<PhysicsSystem>,
+ public entityx::Receiver<PhysicsSystem>
{
private:
+ World* currentWorld;
public:
PhysicsSystem(void) {}
void update(entityx::EntityManager& entites,
entityx::EventManager& events,
entityx::TimeDelta dt) final;
+
+ void receive(const WorldChangeEvent& wce);
};
#endif // SYSTEM_PHYSICS_HPP_
[[maybe_unused]] entityx::EventManager& events)
{
events.subscribe<WorldMeshUpdateEvent>(*this);
+ events.subscribe<entityx::ComponentAddedEvent<Player>>(*this);
init();
}
***********/
glm::mat4 view = glm::lookAt(camPos, // Pos
- glm::vec3(0.0f, 0.0f, 0.0f), // Facing
+ glm::vec3(camPos.x, camPos.y, 0.0f), // Facing
glm::vec3(0.0f, 1.0f, 0.0f)); // Up
//glm::mat4 projection = glm::perspective(45.0f,
// 0.01f,
// 2048.0f);
- glm::mat4 projection = glm::ortho(-((float)width/2), // Left
- ((float)width/2), // Right
- -((float)height/2), // Bottom
- ((float)height/2), // Top
- -10.0f, // zNear
- 10.0f // zFar
+ float scale = 40.0f;
+ float scaleWidth = static_cast<float>(width) / scale;
+ float scaleHeight = static_cast<float>(height) / scale;
+
+ glm::mat4 projection = glm::ortho(-(scaleWidth/2), // Left
+ (scaleWidth/2), // Right
+ -(scaleHeight/2), // Bottom
+ (scaleHeight/2), // Top
+ 10.0f, // zFar
+ -10.0f // zNear
);
glm::mat4 model = glm::mat4(1.0f);
- model = glm::scale(model, glm::vec3(20.0f, 20.0f, 1.0f));
glUseProgram(s);
GLfloat amb[4] = {1.0f, 1.0f, 1.0f, 0.0f};
glUniform4fv(b, 1, amb);
+ /************
+ * CAMERA *
+ ************/
+ try {
+ if (player.has_component<Position>()) {
+ Position *pos = player.component<Position>().get();
+ camPos.y = pos->y;
+ camPos.x = pos->x;
+ }
+ } catch (...) { // If the player doesn't exist or anything goes wrong
+ camPos.y = 0.0f;
+ camPos.x = 0.0f;
+ }
+
/**************
* LIGHTING *
**************/
-
std::vector<glm::vec3> lightPos;
std::vector<glm::vec4> lightColor;
entities.each<Light, Position>([&]
(entityx::Entity, Light &l, Position &p){
- lightPos.push_back(glm::vec3(p.x, p.y,-10.0));
+ lightPos.push_back(glm::vec3(p.x, p.y, 1.0));
lightColor.push_back(glm::vec4(l.r, l.g, l.b, l.strength));
lightNum++;
});
// e.component<Scripted>()->updateRender();
//}
- float w = r.texture.width/2.0f;
- float h = r.texture.height;
+ float w = 0.5f;
+ float h = (float)r.texture.height/r.texture.width;
GLuint tri_vbo;
GLfloat tri_data[] = {
5*sizeof(float), (void*)(3*sizeof(float)));
glDrawArrays(GL_TRIANGLES, 0, 6);
});
+ glUniform1i(f, 0);
// If we were given a world VBO render it
if (worldVBO) {
worldTexture = wmu.worldTexture;
worldNormal = wmu.worldNormal;
}
+
+void RenderSystem::receive(const entityx::ComponentAddedEvent<Player> &cae)
+{
+ player = cae.entity;
+}
#include "world.hpp"
#include "events/world.hpp"
+#include "components/Player.hpp"
+
class RenderSystem : public entityx::System<RenderSystem>,
public entityx::Receiver<RenderSystem>
{
unsigned int worldVertex = 0;
GLuint worldTexture = 0;
GLuint worldNormal = 0;
+ entityx::Entity player; // Save the player so we can track the camera
public:
RenderSystem() :
window(nullptr, SDL_DestroyWindow) {}
* EVENTS *
************/
void receive(const WorldMeshUpdateEvent &wmu);
+ void receive(const entityx::ComponentAddedEvent<Player> &cae);
};
e.assign<Velocity>(Velocity().FromLua(tab["Velocity"])).get();
}
- if (tab["Player"] != nullptr) {
- (*toRet)["Player"] = e.assign<Player>().get();
- }
-
if (tab["Physics"] != nullptr) {
if (!e.has_component<Position>()) // Position must exist for phys.
(*toRet)["Position"] = e.assign<Position>().get();
e.assign<EventListener>(listeners);
}
+ if (tab["Player"] != nullptr) {
+ (*toRet)["Player"] = e.assign<Player>().get();
+ }
+
} else {
// TODO better logging
std::cerr << "Parameter to spawn() must be a table!" << std::endl;
return seed;
}
+/* PHYSICS */
+double World::getHeight(double x, double y, double z)
+{
+ unsigned int X = static_cast<unsigned int>(x);
+ unsigned int Z = static_cast<unsigned int>(z);
+
+ double Y = 0.0;
+ try {
+ auto &d = data.at(Z).at(X);
+ for (int yi = d.size()-1; yi >= 0; yi--) {
+ if (d.at(yi) >= 0) {
+ if (!registry.at(d.at(yi)).passable) {
+ Y = static_cast<double>(yi);
+ Y += 1;
+ break;
+ }
+ }
+ }
+ } catch (...) { // If we get any errors, just let the character
+ return y;
+ }
+
+ return Y;
+}
+
/******************
* WORLD SYSTEM *
World* WorldSystem::addWorld(sol::object t)
{
worlds.push_back(World(t));
- if (currentWorld == nullptr)
- currentWorld = &(worlds.back());
return &(worlds.back());
}
/* SEED */
unsigned int getSeed();
unsigned int setSeed(unsigned int);
+
+ /* PHYSICS */
+ double getHeight(double x, double y, double z);
};
/**