pacman.c 2.24 KB
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "scene.h"
#include "sdl_wrapper.h"
#include "state.h"

const double WEDGE_ANGLE = M_PI / 3; // angle of pacman's mouth
const double INCREMENT_ANGLE = 0.1;  // angle between points on pacman shape
const double PACMAN_RADIUS = 60;
const double BALL_RADIUS = 10;  // radius of balls (pacman's food)
const size_t CIRC_NPOINTS = 20; // points to draw circle

struct state {
  scene_t *scene;
  // TODO: Add any other fields in state you need for your demo
};

/** Make a circle-shaped body object.
 *
 * @param center a vector representing the center of the body.
 * @param radius the radius of the circle
 * @param mass the mass of the body
 * @param color the color of the circle
 * @return pointer to the circle-shaped body
 */
body_t *make_circle(vector_t center, double radius, double mass,
                    color_t color) {
  list_t *c = list_init(CIRC_NPOINTS, free);

  for (size_t i = 0; i < CIRC_NPOINTS; i++) {
    double angle = 2 * M_PI * i / CIRC_NPOINTS;

    vector_t *v = malloc(sizeof(*v));
    assert(v);

    vector_t unit = {cos(angle), sin(angle)};
    *v = vec_add(vec_multiply(radius, unit), center);

    list_add(c, v);
  }

  return body_init(c, mass, color);
}

/** Return a list of points representing the pacman shape.
 *
 * @param center a vector representing the center of the pacman
 * @return list of vectors representing points of pacman object.
 */
list_t *make_pacman(vector_t center) {
  list_t *points = list_init(1, free);
  vector_t *vec = malloc(sizeof(vector_t));
  assert(vec);

  for (double i = (WEDGE_ANGLE / 2); i <= (2 * M_PI) - (WEDGE_ANGLE / 2);
       i += INCREMENT_ANGLE) {
    vector_t *new_vec = malloc(sizeof(vector_t));
    assert(new_vec);

    vector_t angle = {cos(i), sin(i)};
    *new_vec = vec_add(vec_multiply(PACMAN_RADIUS, angle), center);

    list_add(points, new_vec);
  }

  vector_t *new_vec = malloc(sizeof(vector_t));
  assert(new_vec);
  *new_vec = center;

  list_add(points, new_vec);
  return points;
}

state_t *emscripten_init() {
  // TODO: Initialize the state
}

void emscripten_main(state_t *state) {
  // TODO: Implement the main loop
}

void emscripten_free(state_t *state) {
  // TODO: Free the state
}